【问题标题】:Getting 400 error in a Spring MVC project在 Spring MVC 项目中出现 400 错误
【发布时间】:2017-07-14 18:25:23
【问题描述】:

这是我的 spring-mvc 项目,我正在尝试登录,但无法登录。我已经包含了我创建的所有文件。

下面是我的jsp表单。

      <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
            <%@ taglib uri="http://www.springframework.org/tags"prefix="spring"%>
            <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
            <%@ page session="false" %>
            <html>
            <head>
                <title>Employee Page</title>
            </head>
            <body>
                <form action="reg.htm" method="get">
                Employee name:  <input type="text" name="ename"/>
                Employee ID:  <input type="text" name="empno"/>
                Employee job:  <input type="text" name="job"/>
                <input type="submit" value="success"/>
                </form>
            </body>
            </html>

这是我下面的控制器类。

        package com.SpringMvcHello.Controller;

        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.web.servlet.ModelAndView;

        import com.ycs.bean.Employee;


        @Controller
        public class RegistrationController {


            @RequestMapping(value="/reg.htm", method= RequestMethod.GET)
            public ModelAndView sayHello(@ModelAttribute("e")Employee emp){
                ModelAndView model=new ModelAndView("login1");
                String ename=emp.getEname();
                System.out.println("ename ="+ename);
                return model;

            }


        }

这是我下面的 web.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:aop="http://www.springframework.org/schema/aop"
             xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
            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.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
            <!-- Enables the Spring MVC @Controller programming model -->



         <context:component-scan base-package="com.SpringMvcHello.Controller"/>

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

            <bean name="e" class="com.ycs.bean.Employee"/>

            <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"></bean>



        </beans>

这是我的 dispatchers-servlet 来配置我的 dispatcher servlet。

        <?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_2_5.xsd" version="2.5">
          <display-name>HelloWorld</display-name>
          <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
           </welcome-file-list>




        <!-- Dispatcher Servlet configuration -->


           <servlet>
              <servlet-name>hello</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
           </servlet>
           <servlet-mapping>
              <servlet-name>hello</servlet-name>
              <url-pattern>*.htm</url-pattern> 
           </servlet-mapping>


         <!--   Session configuration -->

             <session-config>
                <session-timeout>
                    30
                </session-timeout>
            </session-config>

        </web-app>

这是视图部分

        <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
            pageEncoding="ISO-8859-1"%>
        <!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=ISO-8859-1">
        <title>Insert title here</title>
        </head>
        <body>
        <h1>
        hello ${e.ename}</h1>  
        Your ID is : ${e.empno}
        Your Name is : ${e.ename}
        Job is : ${e.job}

        </body>
        </html>

【问题讨论】:

    标签: java spring hibernate spring-mvc


    【解决方案1】:

    在您的登录页面中,您有一个表单,其中包含您希望通过 POST 方法发送的数据以进行评估并返回您的登录会话,对吗?

    然后:

    <form action="reg.htm" method="POST">
                    Employee name:  <input type="text" name="ename"/>
                    Employee ID:  <input type="text" name="empno"/>
                    Employee job:  <input type="text" name="job"/>
                    <input type="submit" value="success"/>
    </form>
    

    然后在你的控制器中你需要声明处理它的方法

    @Controller
            public class RegistrationController {
    
    
                @RequestMapping(value="/reg.htm", method= RequestMethod.POST)
                public ModelAndView sayHello(@ModelAttribute("e")Employee emp){
                    //Here you don't need to set a view, you can redirect to another view with the right object model.
                    ModelAndView model=new ModelAndView("redirect:index");
                    String ename=emp.getEname();
                    //Here you can add directly you Employee object to your model
                    model.addAttribute("emp", emp);
                    System.out.println("ename ="+ename);
                    return model;
    
                }
    
    
    }
    

    然后在您的索引视图中,您可以使用 Spring 表达式语言 (SPEL) 调用您的 emp 对象,如下所示:

    <div>My Employee name: ${emp.ename}</div>
    

    【讨论】:

    • 但我正在使用 get
    • 使用表单中的 get 方法,您不会向控制器发送数据,您只是在请求数据。
    【解决方案2】:

    您需要像这样将表单更改为弹簧表单:

    <form:form action="reg.htm" modelAttribute="e">
                Employee name:  <form:input type="text" name="ename"path="ename"/>
                Employee ID:  <form:input type="text" name="empno"path="empno"/>
                Employee job:  <form:input type="text" name="job" path="job"/>
                <input type="submit" value="success"/>
    </form:form>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-04
      • 2017-04-09
      • 1970-01-01
      • 2017-06-28
      相关资源
      最近更新 更多