程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 使用Spring MVC的表單控制器SimpleFormController

使用Spring MVC的表單控制器SimpleFormController

編輯:關於JAVA

以注冊過程為例,我們可能會選擇繼承AbstractController來實現表單的顯示,繼承AbstractCommandController來實現表單的處理 ,這樣是可行的,但必須要維護兩個控制器

在這種情況下,我們應該使用SimpleFormController,他接受GEt請求時顯示表單,接受POST請求時處理表單,如果發生錯誤,控制器會知道重新顯示這個表單,這樣用戶就可以修改錯誤,重新提交

表單對應的POJO

package model;

public class Student ...{
  private String name;
  private String sex;
public String getName() ...{
   return name;
}
public void setName(String name) ...{
   this.name = name;
}
public String getSex() ...{
   return sex;
}
public void setSex(String sex) ...{
   this.sex = sex;
}
}

控制器:

這個base類中還有一個DoSubmitAction()方法,和onSubmit()方法的區別就是後者可以返回一個ModelAndView對象,完成向頁面輸出數據的功能,而前者不能向頁面返回數據,這兩個方法同時只有一個有效

package Action;

import model.Student;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class RegisterStudentController extends SimpleFormController ...{

   public RegisterStudentController()...{
     this.setCommandClass(Student.class);
   }

   protected ModelAndView onSubmit(Object object, BindException arg1) throws Exception ...{
     Student stu=(Student)object;
     return new ModelAndView(getSuccessView(),"student",stu);
   }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
   xmlns="http://java.sun.com/xml/ns/j2ee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/train-service.xml,/WEB-INF/train-data.xml,/WEB-INF/train-servlet.xml</param-value>
  </context-param>
  <servlet>
   <servlet-name>train</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
   <servlet-name>train</servlet-name>
   <url-pattern>*.mvc</url-pattern>
  </servlet-mapping>
  <filter>
   <filter-name>character</filter-name>
   <filter-class>Action.CharacterFilter</filter-class>
  </filter>
  <filter-mapping>
   <filter-name>character</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

train-servlet.xml

formView定義為register對應我們的表單提交頁面register.jsp

successView定義為success對應提交成功的顯示頁面success.jsp

<bean id="RegisterStudentController" class="Action.RegisterStudentController">
  <property name="formView">
   <value>register</value>
  </property>
  <property name="successView">
   <value>success</value>
  </property>
</bean>
<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
  <props>
   <prop key="/home.mvc">HomeController</prop>
   <prop key="/register.mvc">RegisterStudentController</prop>
  </props>
</property>
</bean>

register.jsp: <%...@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%...
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   <base href="<%=basePath%>">
   <title>My JSP 'index.jsp' starting page</title>
   <meta http-equiv="pragma" content="no-cache">
   <meta http-equiv="cache-control" content="no-cache">
   <meta http-equiv="expires" content="0">
   <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
   <meta http-equiv="description" content="This is my page">
   <!--
   <link rel="stylesheet" type="text/css" href="styles.css">
   -->
  </head>
  <body>
  <form action="<%=request.getContextPath() %>/register.mvc" method="post">
   name:<input type="text" name="name"/></br>
   sex:<input type="text" name="sex"/></br>
   <input type="submit" value="submit"/>
  </form>
  </body>
</html>

success.jsp <%...@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%...
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   <base href="<%=basePath%>">
   <title>My JSP 'index.jsp' starting page</title>
   <meta http-equiv="pragma" content="no-cache">
   <meta http-equiv="cache-control" content="no-cache">
   <meta http-equiv="expires" content="0">
   <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
   <meta http-equiv="description" content="This is my page">
   <!--
   <link rel="stylesheet" type="text/css" href="styles.css">
   -->
  </head>
  <body>
   ${student.name}-----${student.sex} <br>
  </body>
</html>

測試運行,可以看到success.jsp上有我們提交的信息

去-----1 (使用filter處理中文問題)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved