当前位置: 代码迷 >> Web前端 >> WebWork2 Property-Driven & Model-Driven 驱动方式
  详细解决方案

WebWork2 Property-Driven & Model-Driven 驱动方式

热度:683   发布时间:2012-10-24 14:15:58.0
WebWork2 Property-Driven & Model-Driven 驱动模式
昨天花了一晚上通读了《webwork in action》第三第四章节,高级输入部分的源码硬是通不过,今天趁着晚上值班空闲的时间,终于验证通过了。下面总结一下:

首先是 Property-Driven (属性依附于Action对象)

web.xml 代码
  1. >
  2. <web-app>
  3. <filter>
  4. <filter-name>webwork<!---->filter-name>
  5. <filter-class>
  6. com.opensymphony.webwork.dispatcher.FilterDispatcher
  7. <!---->filter-class>
  8. <!---->filter>
  9. <filter-mapping>
  10. <filter-name>webwork<!---->filter-name>
  11. <url-pattern>/*<!---->url-pattern>
  12. <!---->filter-mapping>
  13. <taglib>
  14. <taglib-uri>webwork<!---->taglib-uri>
  15. <taglib-location>
  16. /WEB-INF/lib/webwork-2.2.4.jar
  17. <!---->taglib-location>
  18. <!---->taglib>
  19. <!---->web-app>


xwork.xml 代码
  1. >
  2. <xwork>
  3. <include file="webwork-default.xml" />
  4. <package name="default" extends="webwork-default">
  5. <action name="userLogin" class="org.vv.action.UserLogin">
  6. <result name="success">welcome.jsp<!---->result>
  7. <result name="input">index.jsp<!---->result>
  8. <!---->action>
  9. <!---->package>
  10. <!---->xwork>

User 代码
  1. package org.vv.vo;
  2. public class User {
  3. private String userName;
  4. private String userPwd;
  5. public String getUserName() {
  6. return userName;
  7. }
  8. public void setUserName(String userName) {
  9. this.userName = userName;
  10. }
  11. public String getUserPwd() {
  12. return userPwd;
  13. }
  14. public void setUserPwd(String userPwd) {
  15. this.userPwd = userPwd;
  16. }
  17. }
UserDAO.java 代码
  1. package org.vv.dao;
  2. import org.vv.vo.User;
  3. public class UserDAO {
  4. public User findByUser(String userName,String userPwd)
  5. {
  6. User user = null;
  7. if(userName.equals("aaa") && userPwd.equals("bbb")){
  8. user = new User();
  9. user.setUserName(userName);
  10. user.setUserPwd(userPwd);
  11. }
  12. return user;
  13. }
  14. }

UserLogin.java 代码
  1. package org.vv.action;
  2. import java.util.Map;
  3. import org.vv.dao.UserDAO;
  4. import org.vv.vo.User;
  5. import com.opensymphony.xwork.ActionContext;
  6. import com.opensymphony.xwork.ActionSupport;
  7. public class UserLogin extends ActionSupport {
  8. private static final long serialVersionUID = 1L;
  9. User user;
  10. UserDAO userDAO = new UserDAO();
  11. Map
  12. public User getUser() {
  13. return user;
  14. }
  15. public void setUser(User user) {
  16. this.user = user;
  17. }
  18. public UserDAO getUserDAO() {
  19. return userDAO;
  20. }
  21. public void setUserDAO(UserDAO userDAO) {
  22. this.userDAO = userDAO;
  23. }
  24. public String execute() throws Exception {
  25. String userName = user.getUserName();
  26. String userPwd = user.getUserPwd();
  27. if (userDAO.findByUser(userName, userPwd) != null) {
  28. ActionContext ctx = ActionContext.getContext();
  29. session = ctx.getSession();
  30. session.put("userName", userName);
  31. return SUCCESS;
  32. } else {
  33. return INPUT;
  34. }
  35. }
  36. }
index.jsp 代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
  2. <%@ taglib prefix="ww" uri="webwork" %>
  3. <html>
  4. <head>
  5. <title><!---->title>
  6. <!---->head>
  7. <body>
  8. <p align="center">
  9. 登录
  10. <br>
  11. <!---->p>
  12. <form action="userLogin.action" method="post">
  13. userName:
  14. <ww:textfield name="user.userName"/>
  15. userPwd:
  16. <ww:textfield name="user.userPwd"/>
  17. <ww:submit name="submit" value="submit"/>
  18. <!---->form>
  19. <!---->body>
  20. <!---->html>

welcome.jsp 代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
  2. <%@ taglib prefix="ww" uri="webwork" %>
  3. <html>
  4. <head>
  5. <title><!---->title>
  6. <!---->head>
  7. <body>
  8. success!!!<br/>
  9. userName:<ww:property value="user.userName"/><br/>
  10. Password:<ww:property value="user.userPwd"/><br/>
  11. session:<ww:property value="#session['userName']"/>
  12. <!---->body>
  13. <!---->html>


下面是Model-Driven 只要修改两个文件即可
UserLogin.java 代码(实现ModelDriven接口和getModel( )方法)
  1. package org.vv.action;
  2. import java.util.Map;
  3. import org.vv.dao.UserDAO;
  4. import org.vv.vo.User;
  5. import com.opensymphony.xwork.ActionContext;
  6. import com.opensymphony.xwork.ActionSupport;
  7. import com.opensymphony.xwork.ModelDriven;
  8. public class UserLogin extends ActionSupport implements ModelDriven{
  9. private static final long serialVersionUID = 1L;
  10. User user;
  11. UserDAO userDAO = new UserDAO();
  12. Map
  13. public User getUser() {
  14. return user;
  15. }
  16. public void setUser(User user) {
  17. this.user = user;
  18. }
  19. public UserDAO getUserDAO() {
  20. return userDAO;
  21. }
  22. public void setUserDAO(UserDAO userDAO) {
  23. this.userDAO = userDAO;
  24. }
  25. public String execute() throws Exception {
  26. String userName = user.getUserName();
  27. String userPwd = user.getUserPwd();
  28. if (userDAO.findByUser(userName, userPwd) != null) {
  29. ActionContext ctx = ActionContext.getContext();
  30. session = ctx.getSession();
  31. session.put("userName", userName);
  32. return SUCCESS;
  33. } else {
  34. return INPUT;
  35. }
  36. }
  37. public Object getModel() {
  38. return user;
  39. }
  40. }

修改xwork.xml
xwork.xml 代码(增加拦截器 interceptor)
  1. >
  2. <xwork>
  3. <include file="webwork-default.xml" />
  4. <package name="default" extends="webwork-default">
  5. <interceptors>
  6. <interceptor-stack name="modelParamsStack">
  7. <interceptor-ref name="params" />
  8. <interceptor-ref name="model-driven" />
  9. <!---->interceptor-stack>
  10. <!---->interceptors>
  11. <action name="userLogin" class="org.vv.action.UserLogin">
  12. <result name="success">welcome.jsp<!---->result>
  13. <result name="input">index.jsp<!---->result>
  14. <interceptor-ref name="modelParamsStack" />
  15. <!---->action>
  16. <!---->package>
  17. <!---->xwork>

ok!
  相关解决方案