2017-07-14 50 views
-1

这是我的spring-mvc项目,我正在尝试登录,但我无法做到这一点。我已经包含了我创建的所有文件。在Spring MVC项目中获取400错误

所以这是我下面的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> 

回答

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; 

      } 


} 

的方法,然后在你的索引视图,你可以打电话给你春天表达式语言EMP对象(SPEL)是这样的:

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

但我使用得到 –

+0

通过表单中的get方法,您不会将数据发送到控制器,而只是请求数据。 –

0

您需要将窗体更改为像这样的弹簧形式:

<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>