代码 |
…… <servlet> <servlet-name>webwork</servlet-name> <servlet-class>com.opensymphony.webwork.dispatcher.ServletDispatcher</servlet-class> </servlet> …… <servlet-mapping> <servlet-name>webwork</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> …… |
代码 |
package helloWorld import com.opensymphony.xwork.Action; public class HelloWorldAction implements Action{ String greeting; public String getGreeting() { return greeting; } public String execute() throws Exception { greeting = "Hello World!"; return SUCCESS; } } |
代码 |
<%@ taglib prefix="ww" uri="webwork" %> <html> <head> <title>First WebWork Example</title> </head> <body> <p><ww:property value="greeting"/></p> </body> </html> |
代码 |
<action name="hello" class=" helloWorld .HelloWorldAction"> <result name="success" type="dispatcher"> <param name="location">/greetings.jsp</param> </result> </action> |
Action(动作)
Action介绍
Action在MVC模式中担任控制部分的角色,在WebWork中使用的最多。每个请求的动作都对应于一个相应的Action,一个Action是一个独立的工作单元和控制命令,它必需要实现XWork里的Action接口,实现Action接口的execute()方法。Action接口的代码如下:
代码 |
package com.opensymphony.xwork; import java.io.Serializable; public interface Action extends Serializable { public static final String SUCCESS = "success"; public static final String NONE = "none"; public static final String ERROR = "error"; public static final String INPUT = "input"; public static final String LOGIN = "login"; public String execute() throws Exception; } |
代码 |
package register; public class User { private String username; private String password; private String email; private int age; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } …… public int getAge() { return age; } public int setAge(int age) { this.age = age; } public String toString() { return "username=" + username + ";password=" + password + ";email=" + email + ";age=" + age; } } |
代码 |
package example.register; import com.opensymphony.xwork.Action; /** * @author moxie-qac * [email protected] */ public class RegisterAction implements Action { private User user= new User(); public User getUser() { return this.user; } public String execute() { System.out.println("Start execute 。。。。。。。。。。。。。"); System.out.println("User="+user); //在这里调用用户注册的业务逻辑,比如:将注册信息存储到数据库 return SUCCESS; } } |
代码 |
<html> <head><title>Register Example</title></head> <body> <table border=0 width=97%> <tr><td align="left"> <form name="register" action="register.action" method="post"> Username:<input type="text" name="user.username"><br> Password:<input type="text" name="user.password"><br> Email:<input type="text" name="user.email"><br> Age:<input type="text" name="user.age"><br> <input type="submit" name="Submit"><br> </form> </td></tr> </table> </body> </html> |
代码 |
<%@ taglib prefix="ww" uri="webwork" %> <html> <head><title>Register result</title></head> <body> <table border=0 width=97%> <tr> <td align="left"> Congratulation,your register success!<p> Username:<ww:property value="user.username"/><br> Password:<ww:property value="user.password"/><br> Email:<ww:property value="user.email"/><br> Age:<ww:property value="user.age"/><br> </td> </tr> </table> </body> </html> |
代码 |
<action name="register" class="example.register.RegisterAction" > <result name="success" type="dispatcher"> <param name="location">/register-result.jsp</param> </result> <interceptor-ref name="params"/> </action> |
代码 |
package example.register; import com.opensymphony.xwork.Action; import com.opensymphony.xwork.ModelDriven; /** * @author moxie-qac * [email protected] * */ public class RegisterActionModel implements Action,ModelDriven{ private User user = new User(); public String execute() throws Exception { System.out.println("Start execute......。。。。。。。。。。。。。。"); System.out.println("User="+user); //在这里调用用户注册的业务逻辑,比如:将注册信息存储到数据库 return SUCCESS; } public Object getModel() { return user; } } |
代码 |
<html> <head><title>Register Example</title></head> <body> <table border=0 width=97%> <tr><td align="left"> <form name="register" action="registerModel.action" method="post"> Username:<input type="text" name="username"><br> Password:<input type="text" name="password"><br> Email:<input type="text" name="email"><br> Age:<input type="text" name="age"><br> <input type="submit" name="Submit"><br> </form> </td></tr> </table> </body> </html> |
代码 |
<action name="registerModel" class="example.register.RegisterActionModel"> <result name="success" type="dispatcher"> <param name="location">/register-result-model.jsp</param> </result> <interceptor-ref name="model-driven"/> <interceptor-ref name="params"/> </action> |
代码 |
ActionContext context = ActionContext.getContext(); Map params = context.getParameters(); String username = (String) params.get(“username”); |
代码 |
public static ActionContext getContext() { ActionContext context = (ActionContext) actionContext.get(); if (context == null) { OgnlValueStack vs = new OgnlValueStack(); context = new ActionContext(vs.getContext()); setContext(context); } return context; } |
代码 |
if ("true".equalsIgnoreCase(Configuration.getString("webwork.configuration.xml.reload"))) { FileManager.setReloadingConfigs(true); } |
代码 |
/** * 将所有的应用请求和servlet属性保存到一个HashMap中, * @param requestMap 存放所有request请求属性的Map * @param parameterMap 存放所有request请求参数的Map * @param sessionMap存放所有session属性的Map * @param applicationMap 存放所有servlet上下文属性的Map * @param request HttpServletRequest 对象 * @param response HttpServletResponse 对象. * @param servletConfig ServletConfig 对象. * @return代表Action 上下文的一个 HashMap */ public static HashMap createContextMap(Map requestMap, Map parameterMap, Map sessionMap, Map applicationMap, HttpServletRequest request, HttpServletResponse response, ServletConfig servletConfig) { HashMap extraContext = new HashMap(); extraContext.put(ActionContext.PARAMETERS, parameterMap); extraContext.put(ActionContext.SESSION, sessionMap); extraContext.put(ActionContext.APPLICATION, applicationMap); extraContext.put(ActionContext.LOCALE, request.getLocale()); extraContext.put(HTTP_REQUEST, request); extraContext.put(HTTP_RESPONSE, response); extraContext.put(SERVLET_CONFIG, servletConfig); extraContext.put(COMPONENT_MANAGER, request.getAttribute("DefaultComponentManager")); // helpers to get access to request/session/application scope extraContext.put("request", requestMap); extraContext.put("session", sessionMap); extraContext.put("application", applicationMap); extraContext.put("parameters", parameterMap); AttributeMap attrMap = new AttributeMap(extraContext); extraContext.put("attr", attrMap); return extraContext; } |
代码 |
protected Map getParameterMap(HttpServletRequest request) throws IOException { return request.getParameterMap(); } |
代码 |
protected Map getSessionMap(HttpServletRequest request) { return new SessionMap(request); } |
代码 |
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, actionName, extraContext); |
代码 |
invocation = ActionProxyFactory.getFactory().createActionInvocation(this, extraContext); |