85-002-11 在web项目中对Action层进行公共方法的抽取放入到BaseAction中
2016-06-29 12:28
435 查看
图文版:http://note.youdao.com/yws/public/redirect/share?id=abc20790e48da5130e227d9390e4df92&type=false
资源文件下载 https://yunpan.cn/OcRaTMHR3NWIIf 访问密码
4b2c
11.1 在项目启动的时候,struts过滤器已经把jsp常用内置对象和map集合存入到了ActionContext和值栈(ValueStack)中。如果实现了XXXAware接口,就会从相应的ActionContext中获取对应的map进行传入(通过拦截器实现: servletConfig
这个拦截器是在struts-default.xml中配置的)
ServletConfigInterceptor.java
11.2 CategoryAction.java中的Category赋值方式:显示层页面通过拦截器然后由OGNL表达式实现值的注入的下面进行具体步骤的分析。
在CategoryAction.java中定义一个语句,使其显示值栈中的栈顶内容或根节点内容
11.3 让CategoryAction实现数据模型驱动接口
CategoryAction.java
index.jsp
11.4 如果修改action
CategoryAction.java
index.jsp
此时id=null, id1=123
11.5 ModelDriven 同样是通过拦截器(ModelDrivenInterceptor)实现的
ModelDrivenInterceptor.java
11.6 moderDriven是所有的action都可以使用的,所以可以把modelDriven放到baseAction中去同时要使用到泛型
然后在简化CategoryAction.java,这样就只剩下service层的类对象了。
CategoryAction.java
BaseAction.java
11.7 为了进一步简化,现在连CategoryAction中的service也放到baseAction中去。
11.8 为了实现在baseAction中依赖spring注入,需要配置
applicationContext-action.xml
11.9 BaseAction定义好之后现在定义其他action实现类就变得简单了,下面定义
AccountAction.java
11.10 定义
index.jsp
11.11 然后定义struts的配置文件
struts.xml
11.12 然后再定义spring的配置文件
applicationContext-action.xml
资源文件下载 https://yunpan.cn/OcRaTMHR3NWIIf 访问密码
4b2c
11.1 在项目启动的时候,struts过滤器已经把jsp常用内置对象和map集合存入到了ActionContext和值栈(ValueStack)中。如果实现了XXXAware接口,就会从相应的ActionContext中获取对应的map进行传入(通过拦截器实现: servletConfig
这个拦截器是在struts-default.xml中配置的)
ServletConfigInterceptor.java
if(action instanceof RequestAware){ ((RequestAware)action).setRequest((Map)context.get("request")) ; } if(action instanceof SessionAware){ ((SessionAware)action).setSession(context.getSession()) ; } if(action instanceof ApplicationAware){ ((ApplicationAware)action).setApplication(context.getApplication()) ; } |
在CategoryAction.java中定义一个语句,使其显示值栈中的栈顶内容或根节点内容
public String query(){ System.out.println(ActionContext.getContext().getValueStack().getRoot()); } |
CategoryAction.java
public class CategoryAction extends BaseAction implements ModelDriven{ public Object getModel() { //它会把得到的值压入栈顶中(删除category对象的setter、getter方法) Category category = new Category() ; return category; } public String update(){ System.out.println(ActionContext.getContext().getValueStack().getRoot()); System.out.println("==update==") ; System.out.println(categoryService) ; //如果还没有与spring整合,所以输出为null System.out.println(category) ; // categoryService.update(category); return "index" ; } } |
<a href="${pag 4000 eContext.request.contextPath}/category_update.action?id=1&type=儿童休闲&hot=false">ModelDriven驱动程序测试</a> |
CategoryAction.java
private Integer id ; private Integer id1 ; public void setId1(Integer id1) { this.id1 = id1; } public String update(){ System.out.println(ActionContext.getContext().getValueStack().getRoot()); System.out.println("==update==") ; System.out.println(category) ; System.out.println(id) ; System.out.println(id1) ; return "index" ; } |
<a href="${pageContext.request.contextPath}/category_update.action?id=1&type=儿童休闲&hot=false&id1=123">ModelDriven驱动程序测试</a> |
11.5 ModelDriven 同样是通过拦截器(ModelDrivenInterceptor)实现的
ModelDrivenInterceptor.java
public String intercept(ActionInvocation invocation) throws Exception { Object action = invocation.getAction(); if (action instanceof ModelDriven) { ModelDriven modelDriven = (ModelDriven) action; ValueStack stack = invocation.getStack(); Object model = modelDriven.getModel(); if (model != null) { stack.push(model); } } return invocation.invoke(); } |
public class BaseAction<T> extends ActionSupport implements RequestAware,SessionAware,ApplicationAware,ModelDriven<T> { protected T model ; public T getModel() { ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass() ; Class clazz = (Class)type.getActualTypeArguments()[0] ; try { model = (T)clazz.newInstance() ; } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return model; } } |
CategoryAction.java
ublic class CategoryAction extends BaseAction<Category>{ private CategoryService categoryService ; public void setCategoryService(CategoryService categoryService){ this.categoryService = categoryService ; } public String update(){ System.out.println(ActionContext.getContext().getValueStack().getRoot()); System.out.println("==update==") ; System.out.println(model) ; return "index" ; } public String save(){ System.out.println("==save==") ; System.out.println(categoryService) ; //如果还没有与spring整合,所以输出为null return "index" ; } public String query(){ System.out.println(ActionContext.getContext().getValueStack().getRoot()); request.put("categoryList", categoryService.query()) ; session.put("categoryList", categoryService.query()) ; application.put("categoryList", categoryService.query()) ; return "index" ; } } |
public class BaseAction<T> extends ActionSupport implements RequestAware,SessionAware,ApplicationAware,ModelDriven<T> { protected Map<String,Object> request ; protected Map<String,Object> session ; protected Map<String,Object> application ; //以下都是通过ActionContex传递过来的 public void setApplication(Map<String, Object> application) { this.application = application ; } public void setSession(Map<String, Object> session) { this.session = session ; } public void setRequest(Map<String, Object> request) { this.request = request ; } protected T mode e2c4 l ; public T getModel() { ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass() ; Class clazz = (Class)type.getActualTypeArguments()[0] ; try { model = (T)clazz.newInstance() ; } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return model; } }public class BaseAction<T> extends ActionSupport implements RequestAware,SessionAware,ApplicationAware,ModelDriven<T> { protected Map<String,Object> request ; protected Map<String,Object> session ; protected Map<String,Object> application ; //以下都是通过ActionContex传递过来的 public void setApplication(Map<String, Object> application) { this.application = application ; } public void setSession(Map<String, Object> session) { this.session = session ; } public void setRequest(Map<String, Object> request) { this.request = request ; } protected T model ; public T getModel() { ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass() ; Class clazz = (Class)type.getActualTypeArguments()[0] ; try { model = (T)clazz.newInstance() ; } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return model; } } |
protected CategoryService categoryService ; protected AccountService accountService; public void setAccountService(AccountService accountService){ this.accountService = accountService ; } public void setCategoryService(CategoryService categoryService){ this.categoryService = categoryService ; } |
applicationContext-action.xml
<bean id="categoryAction" class="cn.it.shop.action.CategoryAction" scope="prototype" parent="baseAction"/> <bean id="baseAction" class="cn.it.shop.action.BaseAction" scope="prototype"> <property name="categoryService" ref="categoryService"/> <property name="accountService" ref="accountService"/> </bean> |
AccountAction.java
public class AccountAction extends BaseAction<Account>{ public String query(){ System.out.println(model); return "index" ; } } |
index.jsp
<a href="account_query.action?id=10&login=zz">account</a> <br> ${id }<br>${login }<br>${name}<br>${pass }<br> |
struts.xml
<action name="account_*" class="accountAction" method="{1}"> <result name="index">/index.jsp</result> </action> |
applicationContext-action.xml
<bean id="accountAction" class="cn.it.shop.action.AccountAction" scope="prototype" parent="baseAction"/> |
相关文章推荐
- 为什么每个程序员都应该懂点前端知识?
- iOS之百度地图使用篇
- html,css元素居中
- java类的访问权限
- HDU1518:Square
- 85-002-10 在web项目中使用RequestAware接口
- Android热更新微信实践地址
- 85-002-9 在web项目中实现公共管理员模块Account
- 基于Python3 神经网络的实现
- 进制转换
- IOS 属性关键字
- 85-002-8 在web项目中对service层的方法抽取
- 14.FOR You
- 基于Python3 神经网络的实现
- extundelete恢复被删除的文件
- Linux常用命令--sort
- cocos2d-x lua mvc模式
- MySQL 插入数据时,中文乱码问题的解决
- iOS开发学习笔记:基础篇
- 最省最小最牛软件推荐(每日更新)