昨天花了一晚上通读了《webwork in action》第三第四章节,高级输入部分的源码硬是通不过,今天趁着晚上值班空闲的时间,终于验证通过了。下面总结一下:
首先是 Property-Driven (属性依附于Action对象)
web.xml 代码
- >
- <web-app>
- <filter>
- <filter-name>webwork<!---->filter-name>
- <filter-class>
- com.opensymphony.webwork.dispatcher.FilterDispatcher
- <!---->filter-class>
- <!---->filter>
- <filter-mapping>
- <filter-name>webwork<!---->filter-name>
- <url-pattern>/*<!---->url-pattern>
- <!---->filter-mapping>
- <taglib>
- <taglib-uri>webwork<!---->taglib-uri>
- <taglib-location>
- /WEB-INF/lib/webwork-2.2.4.jar
- <!---->taglib-location>
- <!---->taglib>
- <!---->web-app>
xwork.xml 代码
- >
- <xwork>
- <include file="webwork-default.xml" />
- <package name="default" extends="webwork-default">
- <action name="userLogin" class="org.vv.action.UserLogin">
- <result name="success">welcome.jsp<!---->result>
- <result name="input">index.jsp<!---->result>
- <!---->action>
- <!---->package>
- <!---->xwork>
User 代码
- package org.vv.vo;
- public class User {
- private String userName;
- private String userPwd;
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getUserPwd() {
- return userPwd;
- }
- public void setUserPwd(String userPwd) {
- this.userPwd = userPwd;
- }
- }
UserDAO.java 代码
- package org.vv.dao;
- import org.vv.vo.User;
- public class UserDAO {
- public User findByUser(String userName,String userPwd)
- {
- User user = null;
- if(userName.equals("aaa") && userPwd.equals("bbb")){
- user = new User();
- user.setUserName(userName);
- user.setUserPwd(userPwd);
- }
- return user;
- }
- }
UserLogin.java 代码
- package org.vv.action;
- import java.util.Map;
- import org.vv.dao.UserDAO;
- import org.vv.vo.User;
- import com.opensymphony.xwork.ActionContext;
- import com.opensymphony.xwork.ActionSupport;
- public class UserLogin extends ActionSupport {
- private static final long serialVersionUID = 1L;
- User user;
- UserDAO userDAO = new UserDAO();
- Map
- public User getUser() {
- return user;
- }
- public void setUser(User user) {
- this.user = user;
- }
- public UserDAO getUserDAO() {
- return userDAO;
- }
- public void setUserDAO(UserDAO userDAO) {
- this.userDAO = userDAO;
- }
- public String execute() throws Exception {
- String userName = user.getUserName();
- String userPwd = user.getUserPwd();
- if (userDAO.findByUser(userName, userPwd) != null) {
- ActionContext ctx = ActionContext.getContext();
- session = ctx.getSession();
- session.put("userName", userName);
- return SUCCESS;
- } else {
- return INPUT;
- }
- }
- }
index.jsp 代码
- <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
- <%@ taglib prefix="ww" uri="webwork" %>
- <html>
- <head>
- <title><!---->title>
- <!---->head>
- <body>
- <p align="center">
- 登录
- <br>
- <!---->p>
- <form action="userLogin.action" method="post">
- userName:
- <ww:textfield name="user.userName"/>
- userPwd:
- <ww:textfield name="user.userPwd"/>
- <ww:submit name="submit" value="submit"/>
- <!---->form>
- <!---->body>
- <!---->html>
welcome.jsp 代码
- <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
- <%@ taglib prefix="ww" uri="webwork" %>
- <html>
- <head>
- <title><!---->title>
- <!---->head>
- <body>
- success!!!<br/>
- userName:<ww:property value="user.userName"/><br/>
- Password:<ww:property value="user.userPwd"/><br/>
- session:<ww:property value="#session['userName']"/>
- <!---->body>
- <!---->html>
下面是Model-Driven 只要修改两个文件即可
UserLogin.java 代码(实现ModelDriven接口和getModel( )方法)
- package org.vv.action;
- import java.util.Map;
- import org.vv.dao.UserDAO;
- import org.vv.vo.User;
- import com.opensymphony.xwork.ActionContext;
- import com.opensymphony.xwork.ActionSupport;
- import com.opensymphony.xwork.ModelDriven;
- public class UserLogin extends ActionSupport implements ModelDriven{
- private static final long serialVersionUID = 1L;
- User user;
- UserDAO userDAO = new UserDAO();
- Map
- public User getUser() {
- return user;
- }
- public void setUser(User user) {
- this.user = user;
- }
- public UserDAO getUserDAO() {
- return userDAO;
- }
- public void setUserDAO(UserDAO userDAO) {
- this.userDAO = userDAO;
- }
- public String execute() throws Exception {
- String userName = user.getUserName();
- String userPwd = user.getUserPwd();
- if (userDAO.findByUser(userName, userPwd) != null) {
- ActionContext ctx = ActionContext.getContext();
- session = ctx.getSession();
- session.put("userName", userName);
- return SUCCESS;
- } else {
- return INPUT;
- }
- }
- public Object getModel() {
- return user;
- }
- }
修改xwork.xml
xwork.xml 代码(增加拦截器 interceptor)
- >
- <xwork>
- <include file="webwork-default.xml" />
- <package name="default" extends="webwork-default">
- <interceptors>
- <interceptor-stack name="modelParamsStack">
- <interceptor-ref name="params" />
- <interceptor-ref name="model-driven" />
- <!---->interceptor-stack>
- <!---->interceptors>
- <action name="userLogin" class="org.vv.action.UserLogin">
- <result name="success">welcome.jsp<!---->result>
- <result name="input">index.jsp<!---->result>
- <interceptor-ref name="modelParamsStack" />
- <!---->action>
- <!---->package>
- <!---->xwork>
ok!