[SPRING] 봄 MVC HTTP 상태 400 - 잘못된 요청
SPRING봄 MVC HTTP 상태 400 - 잘못된 요청
Spring MVC에서 프로젝트를 실행하려고합니다. 여기에 코드가있다.
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome to Spring Web MVC project</title>
</head>
<body>
<h1>Spring 3 Register!</h1>
<a href="register.htm">click</a>
<form:form action="${pageContext.request.contextPath}/register" method="POST" modelAttribute="userForm">
<table>
<tr>
<td colspan="2" align="center">Spring MVC Form Demo - Registration</td>
</tr>
<tr>
<td>User Name</td>
<td><form:input path="username" /></td>
</tr>
<tr>
<td>Password</td>
<td><form:password path="password" /></td>
</tr>
<tr>
<td>Email</td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td>BirthDate (mm/dd/yyyy)</td>
<td><form:input path="birthDate" /></td>
</tr>
<tr>
<td>Profession</td>
<td><form:select path="profession" items="${professionList}" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Register" /></td>
</tr>
</table>
</form:form>
</body>
</html>
RegistrationController.java
package RegisterInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
*
* @author Harshit Shrivastava
*/
import RegisterInfo.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.Model;
@Controller
@RequestMapping(value = "/register")
public class RegistrationController {
@RequestMapping(method = RequestMethod.GET)
public String viewRegistration(Model model)
{
User userForm = new User();
model.addAttribute("userForm", new User());
List<String> professionList = new ArrayList();
professionList.add("Developer");
professionList.add("Designer");
professionList.add("IT Manager");
model.put("professionList", professionList);
return "index";
}
@RequestMapping(method = RequestMethod.POST)
public String processRegistration(@ModelAttribute("userForm") User user, Map<String, Object> model)
{
System.out.println("Username : " + user.getUserName());
model.put("userForm", new User());
return "index";
}
}
User.java
package RegisterInfo.model;
/**
*
* @author Harshit Shrivastava
*/
import java.util.Date;
public class User {
private String username;
private String password;
private String email;
private Date birthDate;
private String profession;
public String getUserName()
{
return username;
}
public void setUserName(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public Date getBirthDate()
{
return birthDate;
}
public void setBirthDate(Date birthDate)
{
this.birthDate = birthDate;
}
public String getProfession()
{
return profession;
}
public void setProfession(String profession)
{
this.profession = profession;
}
}
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd ">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<context:component-scan base-package="RegisterInfo" />
<mvc:annotation-driven />
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
위의 프로그램에서 양식을 제출할 때마다 항상이 오류가 발생합니다.
오류:
HTTP Status 400 - Bad Request
The request sent by the client was syntactically incorrect
코드에 어떤 문제가 있습니까?
해결법
-
==============================
1.HTTP POST를 제출할 때 날짜를 바인드해야합니다. Spring은 이것이 Date라는 것을 모르고, String으로 본다.
HTTP POST를 제출할 때 날짜를 바인드해야합니다. Spring은 이것이 Date라는 것을 모르고, String으로 본다.
이걸 더해:
@InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); sdf.setLenient(true); binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true)); }
귀하의 컨트롤러에.
-
==============================
2.사용중인 코드에 따라
사용중인 코드에 따라
<prop key="index.htm">indexController</prop> <a href="register.htm">click</a>
위의 코드에서 맞춤법이 올바르지 않습니다 (HTML 대신 htm).
<prop key="index.html">indexController</prop> <a href="register.html">click</a>
from https://stackoverflow.com/questions/34803874/spring-mvc-http-status-400-bad-request by cc-by-sa and MIT license
'SPRING' 카테고리의 다른 글
[SPRING] JPQL에서 별칭을 사용하는 방법 (0) | 2019.05.16 |
---|---|
[SPRING] 요청 URL을 기반으로 다른 URL에 동일한 URL 매핑 (0) | 2019.05.16 |
[SPRING] Spring은 상태 비 저장 세션 관리에도 불구하고 JSESSIONID를 추가한다. (0) | 2019.05.15 |
[SPRING] javax.naming.NameNotFoundException : 이름 [comp / env]가이 Context에 바인드되어 있지 않다 (0) | 2019.05.15 |
[SPRING] json을 객체 목록으로 변환 (0) | 2019.05.15 |