简体   繁体   English

Spring 丢失 contextPath

[英]Spring losting contextPath

I have simple controller which must save my form in DB.我有一个简单的控制器,它必须将我的表单保存在数据库中。 I tried mapping command "/tests/review/saveChanges" in this controller but when I push button "save" I have "HTTP Status 400 – Bad Request".我在这个控制器中尝试了映射命令“/tests/review/saveChanges”,但是当我按下“保存”按钮时,我有“HTTP Status 400 – Bad Request”。

My controller:我的控制器:

@Controller
@RequestMapping(value = "/tests")
public class TestController {

private TestInterfaceService testService;

@Autowired(required = true)
@Qualifier(value = "testService")
public void setTestService(TestInterfaceService testService) {
    this.testService = testService;
}

//here we get list objects
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String listTests(Model model) {

    model.addAttribute("test", new Test());
    model.addAttribute("listTests", this.testService.listTests());
    return "proposals";
}

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addTest(Test t) {
    if (t.getId() == 0) {
        this.testService.addTest(t);
    } else {
        // existing person, call update
        this.testService.updateTest(t);
    }
    return "redirect:/tests/list";
}

@RequestMapping(value = "/remove/{id}")
public String removeTest(@PathVariable("id") long id) {
    this.testService.removeTest(id);
    return "redirect:/tests/list";
}

//Here we start work with specific object
@RequestMapping("/review/{id}")
public String editTest(@PathVariable("id") long id, Model model) {
    Test test = this.testService.getFullTestById(id);
    model.addAttribute("candidateTest", test);
    model.addAttribute("candidateQuestions", test.getQuestions());

    model.addAttribute("candidateAnswers", test.getQuestions());
    return "review";
}

//Here I'm try save all changes
@ModelAttribute("candidateTest")
@RequestMapping(value = "/review/saveChanges", method = RequestMethod.POST)
public String review(@PathVariable("candidateTest") Test candidateTest, Model model) {
    testService.addTest(candidateTest);
    return "redirect:/tests/list";
}

@RequestMapping(value = "/choise/{id}/{status}", method = RequestMethod.GET)
public String choise(@PathVariable("id") long id, @PathVariable("status") String status, Model model) {
    Test test = this.testService.getTestById(id);
    test.setStatus(TestStatus.Developing.getStatus(status));
    this.testService.updateTest(test);
    model.addAttribute("test", this.testService.getTestById(id));
    model.addAttribute("questions", this.testService.getListQuestionsById(id));
    return editTest(id, model);
}

@RequestMapping(value = "/previewTest/{id}")
public String previewTest(@PathVariable("id") long id, Model model) {
    Test test = this.testService.getTestById(id);
    model.addAttribute("ourTest", test);
    return "previewTest";
}
}

My jsp review:我的 jsp 评论:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ page session="true"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<c:set var="contextPath" value="${pageContext.request.contextPath}" />
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet"
href="${contextPath}/resources/css/bootstrap.min.css">
<script src="${contextPath}/resources/js/jquery-3.2.1.min.js"></script>
<script src="${contextPath}/resources/js/bootstrap.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 align="left">
    <c:url var="formTestUrl" value="/tests/list" />
    <a href="${formTestUrl}"><spring:message code="back.to.proposals" /></a>
</h1>

<span style="float: right"><a href="?lang=ru">ru</a> | <a
    href="?lang=ua">ua</a> | <a href="?lang=en">en</a> </span>
<br>

//And here we have something whorg I'm guest.
<form:form modelAttribute="candidateTest"
    action="/tests/review/saveChanges" method="POST">
    <table>
        <tr>
            <td><spring:message code="test.name" /></td>
            <td><form:input path="name" /></td>
        </tr>
        <tr>
            <td><spring:message code="test.free" /></td>
            <td><form:input path="free" /></td>
        </tr>
        <c:forEach items="${candidateTest.questions}" var="question"
            varStatus="status">
            <tr>
                <td align="center">${status.count}</td>
                <td><input name="questions[${status.index}].text"
                    value="${question.text}" /></td>
            </tr>
            <c:forEach items="${question.answers}" var="answer"
                varStatus="interator">
                <td align="center">${interator.count}</td>
                <td><input name="answers[${interator.index}].answer"
                    value="${answer.answer}" /></td>
            </c:forEach>
        </c:forEach>
    </table>
    <br />
    <input type="submit" value="Save" />
</form:form>


<div class="container">
    <div class="btn-group">
        <button type="button" class="btn btn-primary">
            <spring:message code="choise" />
        </button>
        <button type="button" class="btn btn-primary dropdown-toggle"
            data-toggle="dropdown">
            <span class="caret"></span>
        </button>
        <ul class="dropdown-menu" role="menu">
            <li><a
                href="<c:url value='/tests/choise/${ourTest.id}/${"app"}' />"><spring:message
                        code="aprove" /></a></li>
            <li><a
                href="<c:url value='/tests/choise/${ourTest.id}/${"pro"}' />"><spring:message
                        code="return" /></a></li>
            <li><a
                href="<c:url value='/tests/choise/${ourTest.id}/${"dis"}' />"><spring:message
                        code="refuse" /></a></li>
        </ul>
    </div>
</div>
</body>
</html>

When we start review one test we have specific link "http://localhost:8089/Diplom/tests/review/1", then when we push save-button we have this link "http://localhost:8089/tests/review/saveChanges", "Diplom" it is our contextPath which lost.当我们开始审查一个测试时,我们有特定的链接“http://localhost:8089/Diplom/tests/review/1”,然后当我们按下保存按钮时,我们有这个链接“http://localhost:8089/tests/ review/saveChanges", "Diplom" 这是我们丢失的 contextPath。 Of course we can change in jsp link like ${contextPath}/tests/review/saveChanges and I did it.当然,我们可以更改${contextPath}/tests/review/saveChanges等 jsp 链接,我做到了。 And it didn't help.它没有帮助。

======== UPD ========更新

web.xml网站.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/appconfig-root.xml</param-value>
</context-param>
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<filter>
    <filter-name>CharsetFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

spring mvc config xml: spring mvc 配置 xml:

e<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<mvc:annotation-driven />

<mvc:resources mapping="/resources/**" location="/resources/" />

<!--<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <property name="basenames"> <list> <value>classpath:validation</value> </list> 
    </property> </bean> -->


<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="/WEB-INF/locales/messages" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeChangeInterceptor"
    class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="lang" />
</bean>
<bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <property name="defaultLocale" value="en" />
</bean>
<bean id="handlerMapping"
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
        <ref bean="localeChangeInterceptor" />
    </property>
</bean>
<mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>
</mvc:interceptors>

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>
</beans>

Though it was asked a long time ago...虽然很久以前就有人问过...

Instead of ${contextPath}/tests/review/saveChanges而不是${contextPath}/tests/review/saveChanges

Try尝试

${pageContext.request.contextPath}/tests/review/saveChanges

Let me know if it worked!让我知道它是否有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM