HTTP状态400 - 错误请求问题

时间:2017-03-03 14:17:48

标签: spring jsp spring-mvc

我创建简单的Spring应用程序。我有两个视图(格式,确认),模型(用户)和Spring Bean-Controller(Main_Controller)。当我在地址http://localhost:8080/Path/forma.htm上并填写所有格式字段时提交格式玻璃鱼说HTTP状态400 - 错误请求。为什么?要正常工作,我应该看到一个包含客户端条目的确认页面。

Main_Controller

package kontroleri;    

@RequestMapping(value = "/forma",method = RequestMethod.GET)
public String pozivanjeForme(Model model)
{
    model.addAttribute("user",new User());
    return "forma";
}


@RequestMapping(value="/forma",method= RequestMethod.POST)
public String confirm(@ModelAttribute("user")User user,ModelMap model)
{
    model.addAttribute("first_name",user.getFirst_name());
    model.addAttribute("last_name",user.getLast_name());
    model.addAttribute("date_of_birth",user.getDate_of_birth());
    model.addAttribute("pid",user.getPid());
    model.addAttribute("email",user.getEmail());
    model.addAttribute("country",user.getCountry());
    model.addAttribute("city",user.getCity());
    model.addAttribute("postal",user.getPostal());

    return "confirmation";
}

}

用户

package model;
private String first_name;
private String last_name;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate date_of_birth;
private String pid;
private String email;
private String country;
private String city;
private String postal;

public void setFirst_name(String first_name) {
    this.first_name = first_name;
}

public void setPid(String pid) {
    this.pid = pid;
}

public String getCity() {
    return city;
}

public String getCountry() {
    return country;
}

public LocalDate getDate_of_birth() {
    return date_of_birth;
}

public String getEmail() {
    return email;
}

public String getFirst_name() {
    return first_name;
}

public String getLast_name() {
    return last_name;
}

public String getPid() {
    return pid;
}

public String getPostal() {
    return postal;
}

public void setCountry(String country) {
    this.country = country;
}

public void setCity(String city) {
    this.city = city;
}

public void setDate_of_birth(LocalDate date_of_birth) {
    this.date_of_birth = date_of_birth;
}

public void setEmail(String email) {
    this.email = email;
}

public void setLast_name(String last_name) {
    this.last_name = last_name;
}

public void setPostal(String postal) {
    this.postal = postal;
}

}

forma.jsp

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Forma page</title>


        <style>
            .field{
                clear:both;
                padding: 5px;
            }
            .field label{
                text-align: left;
                width: 100px;
                float: left;
            }
            .error{
                color: red;
            }
        </style>
    </head>
    <body>
        <form:form action="forma.htm" method="post" commandName="user">
            <div class="field">
                <form:label path="first_name">First name</form:label>
                <form:input path="first_name"/>
            </div>

            <div class="field">
                <form:label path="last_name">Last name</form:label>
                <form:input path="last_name"/>
            </div>

            <div class="field">
                <form:label path="date_of_birth">Date of birth</form:label>
                <form:input path="date_of_birth" type="date"/>
            </div>

            <div class="field">
                <form:label path="pid">Personal ID</form:label>
                <form:input path="pid"/>
            </div>

            <div class="field">
                <form:label path="email">Email</form:label>
                <form:input path="email"></form:input>
                </div>

            <div class="field">
                <form:label path="country">Country</form:label>
                <form:input path="country"></form:input>
            </div>
            
            <div class="field">
                <form:label path="city">City</form:label>
                <form:input path="city"/>
            </div>
            
            <div class="field">
                <form:label path="postal">Postal code</form:label>
                <form:input path="postal"></form:input>
            </div>
            
            <input type="submit" value="Submit">

        </form:form>
    </body>
</html>

confirmation.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Confirmation Page</title>
    </head>
    <body>
        <h1>You entered following data:</h1>
        <p>First name:${first_name}</p>
        <p>Last name:${last_name}</p>
        <p>Date of birth:${date_of_birth}</p>
        <p>Personal ID:${pid}</p>
        <p>Email:${email}</p>
        <p>Country:${country}</p>
        <p>City:${city}</p>
        <p>Postal code: ${postal}</p>
    </body>
</html>

调度-servlet.xml中

<?xml version='1.0' encoding='UTF-8' ?>

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="index.htm">indexController</prop>
            <prop key="forma.htm">Kontroler</prop>
        </props>
    </property>
</bean>
<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

<bean name="indexController"
      class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="index" />

<bean class="kontroleri.Main_Controller" id="Kontroler"/>         

1 个答案:

答案 0 :(得分:1)

请在dispatcher-servlet.xml中定义额外的命名空间

<beans
    ... 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    ...
    xsi:schemaLocation="
    ...
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

添加<mvc:annotation-driven/>以启用注释驱动并支持格式化日期等字段。