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>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值