2009.12.21——邓讲的关于SpringMVC
在2009.12.16——谢讲的关于项目中,谢讲了一些关于springMVC的,今天邓又讲了,这里就主要介绍下那个Ctroller的写法吧
1.继承org.srpingframework.web.servlet.mvc.Controller接口(注意这是接口)
实现方法:
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception{
。。。。。。
}
看例子:
java
package com.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class HelloController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String success;
//request.setAttribute("hello","Hello zhangsan!");
/*A代码
Map map = new HashMap();
map.put("test_1", "test_model_1");
map.put("test_2", "test_model_2");*/
ModelAndView mav=new ModelAndView("index.jsp");
mav.addObject("test_1", "test_model_113");
mav.addObject("test_2", "test_model_213");
//mav.addAllObjects(map);//可以这样直接加一个map,但前提是A代码要有
return mav;
}
}
注意,这个MOdelAndView生成对象时,要有一个它转向的地址,它有七种构造方法,最常用的是:
ModelAndView mav=new ModelAndView("hello.do","mengmeng","123123");
第一个参数是要转向的地址,
第二个参数是它的名字,前台就通过这个名字来获得第三个参数
第三个参数很显然,是它的值
然后,看一下ApplicationContext.xml里面的配置
<bean id="show" class="com.controller.HelloController"/>
页面
<body>
<%=request.getAttribute("test_1") %><br>
</body>
2.org.springframework.web.servlet.mvc.multiaction.MultiActionController(注意这是类)
它可以在一个Controller类中定义多个方法,并根据用户的请求来执行当中的某个方法。
要使用MultiActionController,你要配一个
org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver或org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResovler的实例,两个类都是根据一个请求参数,以参数决定要执行Controller类中的哪个方法
2.1 org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver
看例子:
java:
package com.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
/**
* MultiActionController 继承该方法的类 可以在类中写多个方法,实现Controller的类中默认实行的方法是
* handleRequest,MultiActionController相当于多个Controller 比较常用。
* 在配置的时候需要注意:
* 该类需要客户端传递个参数
* 1.参数值对应的值就是MultiActionController中想对应的方法
* 2.参数名在ApplicationContext.xml中的ParameterMethodNameResoler中定义
* @author Administrator
*
*/
public class MultiActionControllerTest extends MultiActionController {
public ModelAndView test_01(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String name = request.getParameter("userName");
System.out.println("参数为:" + name);
request.setAttribute("userName", name);
return new ModelAndView("MutilAction_01.jsp");
}
public ModelAndView test_02(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String password = request.getParameter("password");
System.out.println("参数为:" + password);
request.setAttribute("password", password);
return new ModelAndView("MutilAction_02.jsp");
}
}
xml
<bean id="urlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/action1.do">multiAction</prop>
<prop key="/action2.do">multiAction</prop>
</props>
</property>
</bean>
<!-- ParameterMethodNameResover,用来指定MultiActionController中那个参数和方法名相对应上 -->
<bean id="paraMethodResoler"
class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName">
<value>mengmeng</value>
</property>
</bean>
<!-- 这里配置MultiActionController 需要指明 参数methodNameResolver -->
<bean id="multiAction" class="com.controller.MultiActionControllerTest">
<property name="methodNameResolver">
<ref bean="paraMethodResoler"/>
</property>
</bean>
html
<body>
<div align="center">
<h1>MultiActionController Demo</h1>
</div>
<hr>
<div align="center">
<a href="${pageContext.request.contextPath}/action1.do?mengmeng=test_01&userName=zhangsa"> 按钮一</a>
<a href="${pageContext.request.contextPath}/action2.do?mengmeng=test_02&password=123456">按 钮二</a>
</div>
</body>
2.2 org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResovler.
看例子
java
public class YuliangController {
private YuliangService yuliangService;
private String nationalID;
public YuliangService getYuliangService() {
return yuliangService;
}
public void setYuliangService(YuliangService yuliangService) {
this.yuliangService = yuliangService;
}
public String getNationalID() {
return nationalID;
}
public void setNationalID(String nationalID) {
this.nationalID = nationalID;
}
public ModelAndView test1(HttpServletRequest request,
HttpServletResponse response) throws Exception {
PrintWriter out = response.getWriter();
List list = yuliangService.getSubDT_RGNCD("360000");
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(nationalID);
out.print(jsonArray.toString());
return null;
}
public ModelAndView test2(HttpServletRequest request,
HttpServletResponse response) throws Exception {
PrintWriter out = response.getWriter();
return null;
}
}
xml
<!-- 数据库外部文件配置 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list><value>classpath:db.properties</value></list>
</property>
<property name="fileEncoding" value="utf-8" />
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/yuliang*.do">yuliangController</prop>
</props>
</property>
</bean>
/*负责给那个Controller注入全局变量*/
<bean id="yuliangDelegate" class="com.controller.YuliangController">
<property name="yuliangService"><ref bean="yuliangService"/></property>
<property name="nationalID" value="${nationalID}"/>
</bean>
/*这个yuliangController有两部分组成methodNameResolver和delegatea*/
<bean id="yuliangController" class="org.springframework.web.servlet.mvc.multiaction.MultiActionController">
<property name="methodNameResolver" ref="propMethodNameResolver"/>
<property name="delegate" ref="yuliangDelegate"/>
</bean>
/*路径和Controller中方法的对应关系*/
<bean id="propMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/yuliangtest1.do">test1</prop>
<prop key="/yuliangtest2.do">test2</prop>
</props>
</property>
</bean>
html
<body>
<div align="center">
<h1>MultiActionController Demo</h1>
</div>
<hr>
<div align="center">
<a href="${pageContext.request.contextPath}/yuliangtest1.do"> 按钮一</a>
<a href="${pageContext.request.contextPath}/yuliangtest2.do"> 按钮二</a>
</div>
</body>