我有简单的控制器必须在DB中保存我的表单。我尝试在这个控制器中映射命令“/ tests / review / saveChanges”但是当我按下“保存”按钮时,我有“HTTP状态400 - 错误请求”。
我的控制器:
@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";
}
}
我的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>
当我们开始审核一个测试时,我们有特定的链接“http://localhost:8089/Diplom/tests/review/1”,然后当我们按下保存按钮时,我们有这个链接“http://localhost:8089/tests/review/saveChanges”,“Diplom”这是我们的上下文路径丢失了。当然,我们可以像${contextPath}/tests/review/saveChanges
这样在jsp链接中进行更改。它没有帮助。
====== UPD
web.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:
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>
答案 0 :(得分:0)
虽然很久以前就有人问过...
代替
${contextPath}/tests/review/saveChanges
试试
${pageContext.request.contextPath}/tests/review/saveChanges
让我知道它是否有效!