2017-02-10 48 views
1

我試圖用彈簧,引導其得到一個像 無論BindingResult也不是爲bean名稱「命令」平原目標對象可以作爲請求屬性
如何在記錄插入數據庫時​​顯示註冊成功消息?如何使用Spring形式引導和顯示成功消息Spring MVC中

這是映射錯誤,我應該有正確的 後引入spring標籤本身正在使用正常的html和窗體工作正常。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"> 

<html> 
<div id="page-wrapper"> 
      <div class="container-fluid"> 
       <div class="row"> 
        <div class="col-lg-12"> 
         <h3 class="page-header"><b>Student Enrollment</b></h3> 
        </div> 
      <div class="col-lg-12"> 
        <div class="panel panel-default"> 
         <div class="panel-heading"> 
        Basic Information for admin 
         </div> 
         <!-- /.panel-heading --> 
         <div class="panel-body"> 
          <div class="form-body">        


       <div align="center"> 
     <form:form action="register" method="post"> 
      <table border="0"> 
       <tr> 
        <td colspan="2" align="center"><h2>Spring MVC Form Demo - Registration</h2></td> 
       </tr> 
       <tr> 
        <td>User Name:</td> 
        <td><form:input path="username" /></td> 
       </tr> 
       <tr> 
        <td>Password:</td> 
        <td><form:password path="password" /></td> 
       </tr> 
       <tr> 
        <td>E-mail:</td> 
        <td><form:input path="email" /></td> 
       </tr> 
       <tr> 
        <td>Birthday (mm/dd/yyyy):</td> 
        <td><form:input path="birthDate" /></td> 
       </tr> 

       <tr> 
        <td colspan="2" align="center"><input type="submit" value="Register" /></td> 
       </tr> 
      </table> 
     </form:form> 
    </div> 




          </div> 
         </div> 
         <!-- /.panel-body --> 
        </div> 
        <!-- /.panel --> 
       </div> 
        <!-- /.col-lg-12 --> 
       </div> 
       <!-- /.row --> 
      </div> 
      <!-- /.container-fluid --> 
     </div> 
     <!-- /#page-wrapper --> 
    </div> 

Controller類

@Controller 
@RequestMapping(value = "/register") 
public class RegisterController { 


     @RequestMapping(method = RequestMethod.POST) 
     public String processRegistration(@ModelAttribute User user) {   



      return "RegistrationSuccess"; 
     } 
} 

的web.xml

<?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_3_0.xsd" version="3.0"> 
    <display-name>Spring-Registration</display-name> 
    <welcome-file-list> 
    <welcome-file>StudentEnrollmentFromAdmin.jsp</welcome-file>  
    </welcome-file-list> 

    <!-- Single Servlet --> 
    <servlet> 
     <servlet-name>mvc</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring-config.xml 
     </param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>mvc</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

<listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Loads Spring Security configuration file --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/spring-config.xml, 
     </param-value> 
    </context-param> 

</web-app> 
+0

你什麼時候得到這個異常? – VPK

+0

服務器啓動時 –

+0

在加載您嘗試使用它的頁面之前,您需要添加'modelAttribute'作爲模型的一個屬性。 – VPK

回答

0

您需要添加屬性在控制器類用戶。

@Controller 
@RequestMapping(value = "/register") 
public class RegisterController { 
    @RequestMapping(method = RequestMethod.POST) 
    public String processRegistration(@ModelAttribute User user,Model m) {   
    User user=new User(); 
    m.addAttribute(user); 
    return "RegistrationSuccess"; 
} 
相關問題