Spring学习笔记 02
[b]1.ActionSupport类[/b] org.springframework.web.struts.ActionSupport; 在Struts和Spring整合的列子中,某个Action可以继承这个类 可以帮助在Struts的Action里获取ApplicationContext对象 ApplicationContext ctx =this.getWebApplicationContext(); 这个ac需要先在Struts的配置文件中声明 方式1: 在struts-config.xml里注册 [code="xml"] [/code] 方式2: 在web.xml中注册监听器 [code="xml"] contextConfigLocation /WEB-INF/applicationContext.xml org.springframework.web.context.ContextLoaderListener [/code] 方式3: 在web.xml里注册一个servlet [code="xml"] dd org.springframework.web.context.ContextLoaderServlet 1 [/code] 其中包含启动顺序 [b]2.DelegatingActionProxy类,委派[/b] import org.springframework.web.struts.DelegatingActionProxy; 首先创建一个Action类 public class LoginAction extends Action 其中包含某个属性,比如 UserDao userdao; 并且生成set和get方法 接下来我们可以在struts-config.xml里这么注册 [code="xml"] [/code] 这样 当访问当/login.do的时候 服务器会读取这里的信息 并转交给type="org.springframework.web.struts.DelegatingActionProxy" 这个类,所以依然需要在applicationContext.xml中也注册相应的信息 [code="xml"] [/code] *注意这里的bean name 是需要和 Struts-config的path属性相一致 3.DispatcherServlet 分发Servlet类 这个类是Spring MVC的核心 其地位相等于Struts中的ActionServlet,起到分发请求的作用 故需要在web.xml中配置这个类 [code="java"]org.springframework.web.servlet.DispatcherServlet; public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { return new ModelAndView(this.getPage()); }[/code] web.xml里: [code="xml"] myservlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/applicationContext.xml myservlet *.ctrl [/code] 这样,所有以.ctrl结尾的请求将交给DispatcherServlet来处理 其中init-param告诉这个类ApplicationContext文件的位置. 在ApplicationContext.xml中 : [code="xml"] success.jsp [/code] 这就相当于在Struts-config.xml里配置Action一样的作用 当访问/login.ctrl时,Spring MVC来获取这个LoginControll, 并用IOC来注入LoginControll中需要的对象(比如SessionFactory,DateSource) (其中page时注入的页面跳转地址) [b]4.MultiActionController类[/b] 地位相当于Struts中的DispatcherAction 用于Action的减肥 把功能类似的方法集中在一个Action里 [code="java"]org.springframework.web.servlet.mvc.multiaction.MultiActionController; public class UserController extends MultiActionController {}[/code] 在ApplicationContext.xml中如下配置: [code="xml"] function findUserList [/code] 其中,我们利用IOC向这个分发器中注入一个ParameterMethodNameResolver对象 这个对象中有个paramName属性 需要被设置的,这个属性相当于Struts中DispatcherAction中的parameter="**"设置 defaultMethodName是在请求这个页面的时候默认的使用的方法(这里是findUserList) 访问路径/user.ctrl?function=addUser 继续看下去...慢慢补充