webwork in spring

本文主要讲解webworkspring的集成.

有人说有了webwork就没有必要和spring集成.

也有人说webworkspring集成起来功能更加强大.

本人同意上述两个观点,本文觉得webwork没有必须要和spring集成,当然我这里仅指springMVCwebwork本身就是一个很好的MVC实现.而webwork可以和springSpringMVC以外其的组件集成,像spring的持久层组件.更多的本人也不敢发表言论,大家如果感兴趣可以到网上找找相关的资料.

提示:为了减少视频的录制时间,减少上传文件的空间.本文也是在上几个视频的基础上进行改动.所以在阅读本文之前请先参考上几个视频.可以在本博客上直接下载,webwork国际化这节.<o:p></o:p>

Webwork集成spring

1. 修改 web.xml,为 Web 应用增加相应的 Spring ContextLoaderListener <o:p></o:p>

xml 代码
 
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  5.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  6.     <servlet>  
  7.         <servlet-name>webworkservlet-name>  
  8.         <servlet-class>  
  9.             com.opensymphony.webwork.dispatcher.ServletDispatcher  
  10.         servlet-class>  
  11.     servlet>  
  12.     <servlet-mapping>  
  13.         <servlet-name>webworkservlet-name>  
  14.         <url-pattern>*.actionurl-pattern>  
  15.     servlet-mapping>  
  16.     <listener>  
  17.         <listener-class>  
  18.             org.springframework.web.context.ContextLoaderListener  
  19.         listener-class>  
  20.     listener>  
  21. web-app>  
java 代码
 
  1. package com.lyx.service;  
  2. import com.lyx.bo.User;  
  3.   
  4. public interface IUserService {  
  5.     public boolean isValidUser(User user);  
  6.     }  
  7.   
  8. }  
java 代码
 
  1. package com.lyx.service;  
  2. import com.lyx.bo.User;  
  3.   
  4. public class UserService implements IUserService {  
  5.   
  6.     public boolean isValidUser(User user) {  
  7.         if (user.getUserName().equals("lyx") && user.getUserPwd().equals("lyx"))  
  8.             return true;  
  9.         else  
  10.             return false;  
  11.     }  
  12. }  

4.修改xwork.xml配置文件<o:p></o:p>

xml 代码
 
  1. >  
  2. <xwork>  
  3. <include file="webwork-default.xml" />  
  4.     <package name="action" extends="webwork-default">  
  5.         <action name="login" class="login">  
  6.               
  7.             <result name="success" type="dispatcher">  
  8.                 <param name="location">/success.jspparam>  
  9.             result>  
  10.               
  11.             <result name="error" type="dispatcher">  
  12.                 <param name="location">/error.jspparam>  
  13.             result>  
  14.             <result name="input" type="dispatcher">  
  15.                 <param name="location">/index.jspparam>  
  16.             result>  
  17.   
  18.             <interceptor-ref name="model-driven" />  
  19.             <interceptor-ref name="params" />  
  20.             <interceptor-ref name="validationWorkflowStack" />  
  21.         action>  
  22.     package>  
  23. xwork>  

   5) 添加 spring 框架到项目中

   修改 applicationContext.xml 文件,加入 UserServiceImpl 的配置信息<o:p></o:p>

xml 代码
 
  1. xml version="1.0" encoding="UTF-8"?>  
  2. >  
  3.   
  4. <beans>  
  5.     <bean id="userService" class="com.lyx.service.UserService" singleton="false"/>  
  6.     <bean id="login" class="com.lyx.web.action.LoginAction" singleton="false" >  
  7.         <property name="userService">  
  8.             <ref bean="userService"/>  
  9.         property>  
  10.     bean>  
  11. beans>  

注意:这里的红色字体login一定要和xwork配置文件中的login一样.<o:p></o:p>

   6) 修改 Action <o:p></o:p>

java 代码
 
  1.    LoginAction.java  
  2. package com.lyx.web.action;  
  3. import com.opensymphony.xwork.Action;  
  4. import com.opensymphony.xwork.ActionSupport;  
  5. import com.opensymphony.xwork.ModelDriven;  
  6. import com.test.services.IUserService;  
  7. public class LoginAction extends ActionSupport implements Action,ModelDriven  {  
  8.     private User user = new User();  
  9.     private IUserService userService;  
  10.     public User getUser() {  
  11.         return user;  
  12.     }  
  13.     public String execute() throws Exception {  
  14.         if (userService.isValidUser(user))  
  15.             return this.SUCCESS;  
  16.         else  
  17.             return this.ERROR;  
  18.     }  
  19.     public Object getModel() {  
  20.         return user;  
  21.     }  
  22.   
  23.     public void setUserService(IUserService userService) {  
  24.         this.userService = userService;  
  25.     }  
  26. }  

<o:p></o:p>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值