HTTP状态400-错误的请求问题 [英] HTTP Status 400 - Bad Request issue

查看:123
本文介绍了HTTP状态400-错误的请求问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的Spring应用程序.我有两个视图(forma,confirmation),model(User)和Spring Bean-Controller(Main_Controller). 8080/Path/forma.htm"rel =" nofollow noreferrer> http://localhost:8080/Path/forma.htm 并填写所有格式字段,然后提交格式为玻璃鱼的HTTP状态400-错误的Request.Why ?要正常工作,我应该看到包含客户条目的确认页面.

I create simple Spring app.I have two views(forma,confirmation),model(User) and Spring Bean-Controller(Main_Controller).When I'm on the adress http://localhost:8080/Path/forma.htm and fill all forma fields and submit forma glassfish say HTTP Status 400 - Bad Request.Why?To work well should I see a confirmation page with client entry.

Main_Controller

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

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

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>

dispatcher-servlet.xml

dispatcher-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"/>         

推荐答案

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

Please, define extra namespace in 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/>以启用注释驱动并支持格式化日期等字段.

Add <mvc:annotation-driven/> to enable annotation-driven and support for formatting Date etc. fields.

这篇关于HTTP状态400-错误的请求问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆