在Spring MVC项目中获得400错误

时间:2017-07-14 18:25:24

标签: java spring hibernate spring-mvc

这是我的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>

这是我的调度程序 - servlet来配置我的调度程序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>

2 个答案:

答案 0 :(得分:0)

在您的登录页面中,您有一个表单,其中包含您希望通过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 Expression Language(SPEL)调用emp对象,如下所示:

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

答案 1 :(得分:0)

您需要将表单更改为弹簧形式,如下所示:

class Post < ActiveRecord::Base
  before_save :set_squeezed_content
  validates :squeezed_content, uniqueness: true

  def set_squeezed_content
    self.squeezed_content = content.squeeze
  end
end