Spring MVC-Controller

CommandController(命令控制器)
FormController(表單控制器)

WizardFormController(嚮導表單控制器)



CommandController(命令控制器)

* 需要繼承AbstractCommandController類,並重寫handle方法
* 通過構造方法註冊命令類和命令名稱,如:
public MyCommandController(){
//註冊命令類
this.setCommandClass(Person.class);
//命令名稱
this.setCommandName("person");
}
<!-- 命令控制器 -->
	<bean name="/mycommand.action" class="cn.itcast.springmvc.controller.MyCommandController"></bean>

public class Person {

	private String id;
	private String name;
	private String address;
	/**
	 * @return the id
	 */
	public final String getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public final void setId(String id) {
		System.out.println("正在調用setId方法 ,id=" + id);
		this.id = id;
	}
	/**
	 * @return the name
	 */
	public final String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public final void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the address
	 */
	public final String getAddress() {
		return address;
	}
	/**
	 * @param address the address to set
	 */
	public final void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "{id:"+id+",name:"+name+",address:"+address+"}";
	}
}

/**
 * 自定義命令控制器
 *
 */
public class MyCommandController extends AbstractCommandController {//已不建議使用
	
	/**
	 * 通過構造方法註冊命令類
	 */
	public MyCommandController() {
		this.setCommandClass(Person.class);//註冊命令類
		this.setCommandName("person");//指定命令名稱
	}

	@Override
	protected ModelAndView handle(HttpServletRequest req,
			HttpServletResponse resp, Object o, BindException arg3)
			throws Exception {
		
		Person p = (Person)o;
		System.out.println(p);
		return new ModelAndView("command");
	}

}

訪問http://localhost/springmvc_1/mycommander.action?id=001&name=zhangsan&address=beijing
這些屬性一定要和實體內的屬性保持一致 框架調用的是set方法

後臺打印


FormController(表單控制器)



*需要繼承SimpleFormController類,並重寫doSubmitAction方法

*通過構造方法註冊命令類和命令名稱,如:

  public MyFormController(){

  this.setCommandClass(Person.class);

  this.setCommandName("person");

  }

* 在spring配置文件中對錶單控制器進行配置,如:

  <!-- 表單控制器 -->

  <bean id="myFormController"name="/form.action"  class="cn.itcast.controller.MyFormController">

  <property name="successView"value="success"/>

  <property name="formView"value="personForm"/>

  </bean>




/**
 * 自定義表單控制器
 *
 */
public class MyFormController extends SimpleFormController {
	
	/**
	 * 通過構造方法註冊命令類
	 */
	public MyFormController() {
		this.setCommandClass(Person.class);//註冊命令類
		this.setCommandName("person");//指定命令名稱
	}

	/**
	 * 當提交表單時會調用此方法
	 */
	protected void doSubmitAction(Object command) throws Exception {
		Person p = (Person)command;
		System.out.println("doSubmitAction-----person:" + p);
		super.doSubmitAction(command);
	}

}

<html>
  <head>
    <title>personForm.jsp</title>
  </head>
  
  <body>
    <form action="<%=path %>/myform.action" method="POST">
    	id:<input type="text" name="id"><br>
    	name:<input type="text" name="name"><br>
    	address:<input type="text" name="address"><br>
    	<input type="submit" value="提交">
    </form>
  </body>
</html>
http://localhost/springmvc_1/myform.action
後臺打印
正在調用setId方法 ,id=xx
doSubmitAction-----person:{id:xx,name:xx,address:xx}
注意 這裏兩次 地址不變 原因如下


WizardFormController(嚮導表單控制器)


*需要繼承AbstractWizardFormController類,並重寫processFinish方法

*通過構造方法註冊命令類和命令名稱,如:

public MyWizardFormController(){

  this.setCommandClass(Person.class);

  this.setCommandName("person");

}

* 在spring配置文件中對嚮導表單控制器進行配置,如:

<!--嚮導表單控制器-->

<beanname="/wizard.action"id="myWizardFormControlle"class="cn.itcast.controller.MyWizardFormController">

<propertyname="pages">

<list>

<value>wizard/1</value>

<value>wizard/2</value>

<value>wizard/3</value>

</list>

</property>

</bean>

* 創建jsp頁面,

1.jsp   /WEB-INF/jsp/wizard/1.jsp

  <%@ page language="java"import="java.util.*"pageEncoding="ISO-8859-1"%>

<%

Stringpath = request.getContextPath();

StringbasePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<html>

  <head>

    <title>1.jsp</title>

  </head>

  <body>

   <form action="<%=path %>/wizard.action"method="post">

   name:<input type="text"name="name">

   <input type="submit"name="_target1" value="next">

   <input type="submit"name="_cancel" value="cancel">

   </form>

  </body>

</html>

2.jsp  /WEB-INF/jsp/wizard/2.jsp

<%@page language="java" import="java.util.*"pageEncoding="ISO-8859-1"%>

<%

Stringpath = request.getContextPath();

StringbasePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<html>

  <head>

    <title>2.jsp</title>

  </head>

  <body>

    <form action="<%=path %>/wizard.action"method="post">

   age:<input type="text"name="age">

   <input type="submit"name="_target0" value="back">

   <input type="submit"name="_target2" value="next">

   <input type="submit"name="_cancel" value="cancel">

   </form>

  </body>

</html>

3.jsp   /WEB-INF/jsp/wizard/3.jsp

<%@page language="java" import="java.util.*"pageEncoding="ISO-8859-1"%>

<%

Stringpath = request.getContextPath();

StringbasePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <title>3.jsp</title>

  </head>

  <body>

    <form action="<%=path %>/wizard.action"method="post">

   address:<input type="text"name="address">

   <input type="submit"name="_target1" value="back">

   <input type="submit"name="_finish" value="finish">

   <input type="submit"name="_cancel" value="cancel">

   </form>

  </body>

</html>



**
 * 嚮導表單控制器
 *
 */
public class MyWizardFormController extends AbstractWizardFormController {
	
	@Override
	protected ModelAndView processCancel(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		return new ModelAndView("index");
	}

	public MyWizardFormController() {
		this.setCommandClass(Person.class);
		this.setCommandName("person");
	}

	@Override
	protected ModelAndView processFinish(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, BindException arg3)
			throws Exception {
		System.out.println("processFinish-----");
		return new ModelAndView("success");
	}

}

<!-- 嚮導表單控制器 -->
	<bean name="/mywizardform.action" class="cn.itcast.springmvc.controller.MyWizardFormController">
		<!-- 配置表單頁面 -->
		<property name="pages">
			<list>
				<value>/wizard/1</value>
				<value>/wizard/2</value>
				<value>/wizard/3</value>
			</list>
		</property>
	</bean>
用EL表達式做頁面回顯

<html>
  <head>
    <title>1.jsp</title>
  </head>
  
  <body>
    <form action="<%=path %>/mywizardform.action" method="post">
    	id:<input type="text" name="id" value="${requestScope.person.id }"><br>
    	<input type="submit" name="_cancel" value="取消">
    	<input type="submit" name="_target1" value="下一步">
    </form>
  </body>
</html>

<html>
  <head>
    <title>2.jsp</title>
  </head>
  
  <body>
    <form action="<%=path %>/mywizardform.action" method="post">
    	name:<input type="text" name="name" value="${requestScope.person.name }"><br>
    	<input type="submit" name="_target0" value="上一步">
    	<input type="submit" name="_cancel" value="取消">
    	<input type="submit" name="_target2" value="下一步">
    </form>
  </body>
</html>

<html>
  <head>
    <title>3.jsp</title>
  </head>
  
  <body>
    <form action="<%=path %>/mywizardform.action" method="post">
    	address:<input type="text" name="address" value="${requestScope.person.address }"><br>
    	<input type="submit" name="_target1" value="上一步">
    	<input type="submit" name="_cancel" value="取消">
    	<input type="submit" name="_finish" value="完成">
    </form>
  </body>
</html>




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章