result:输出结果;第个Action返回一个字符串,Struts2根据这个值来决定响应结果
name属性:result的逻辑名。和Actin里的返回值匹配,默认"success"
值 :指定对应的实际资源位置
二、Action中返回其它值如果Action中返回其它扯,result中的Name属性要与之对应才可以找到指定的资源
Action默认定义了一些常量,可以拿来使用
package com.opensymphony.xwork2;public interface Action { 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";}三、type属性
<result-types> <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/> <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/> <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/> <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/> <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/> <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/> <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/> <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/> <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/> <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" /> </result-types>
这个配置显示了几种常见的结果类型,关于这些以及其他结果类型,各自作用简介如下:dispatcher: 将请求转发(forward)到本应用程序中指定的资源(jsp 或Servlet)chain: Action 链式处理,将请求转发(forward)到指定的Actionredirect: 请求重定向到指定的 URL(页面或者Action)redirectAction: 请求重定向到指定的Actionjson:实现Ajax 时返回JSON 对象freemarker:处理FreeMarker 模板httpheader:控制特殊HTTP 行为的结果类型stream:像浏览器发送InputStream 对象,通常用来处理文件下载,还可用于返回Ajax数据velocity:处理Velocity 模板xslt:处理xml/XLST 模板plainText:显示原始文件内容,例如文件源代码
package com.pb.web.action;import com.opensymphony.xwork2.ActionSupport;import com.pb.entity.User;/* * 登录响应action 以javaBean方式接收用户登录输入的用户名和密码 */public class LoginAction extends ActionSupport { PRivate static final long serialVersionUID = 1L; //实例化对象 private User user; //要有execute方法public String execute(){ return SUCCESS;}public String login(){ if(user.getUserName().equals("accp") && user.getPassWord().equals("accp")){ return SUCCESS; }else{ return INPUT; }} public User getUser() { return user;}public void setUser(User user) { this.user = user;}}
<constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="login" class="com.pb.web.action.LoginAction" method="login"> <result name="success" type="redirect"> /loginSuccess.jsp </result> <result name="input" type="dispatcher"> /login.jsp </result> </action>
<package name="user" namespace="/user" extends="struts-default"><action name="login" class="com.pb.web.action.UserAction"><result type="chain"><param name="actionName"> houseAction </param><param name="namespace"> / house </param></result></action></package><package name="house" namespace="/house" extends="struts-default"><action name=" houseAction " class="com.pb.web.action.HouseAction"><result>/houseSuccess.jsp</result></action></package>四、全局结果
package com.pb.web.action;import com.opensymphony.xwork2.ActionSupport;public class HourseAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; public String add(){ System.out.println("执行添加操作!"); try{ if(1==1){ //调用service的方法 throw new Exception(); } }catch (Exception e){ e.printStackTrace(); return ERROR; } return "success"; } public String update(){ System.out.println("执行更新操作!"); try{ if(1==1){ //调用service的方法 throw new Exception(); } }catch (Exception e){ e.printStackTrace(); return ERROR; } return "success"; } public String delete(){ System.out.println("执行删除操作!"); try{ if(1==1){ //调用service的方法 throw new Exception(); } }catch (Exception e){ e.printStackTrace(); return ERROR; } return "success"; } }
<global-results> <result name="error">/error.jsp</result> </global-results><!-- <action name="hourse_*" class="com.pb.web.action.HourseAction" method="{1}"> <result name="success" type="dispatcher"> {1}success.jsp </result> </action> --> <action name="hourse_add" class="com.pb.web.action.HourseAction" method="add"> <result name="success" type="dispatcher"> /loginSuccess.jsp </result> </action> <action name="hourse_update" class="com.pb.web.action.HourseAction" method="update"> <result name="success" type="dispatcher"> /loginSuccess.jsp </result> </action> <action name="hourse_delete" class="com.pb.web.action.HourseAction" method="delete"> <result name="success" type="dispatcher"> /loginSuccess.jsp </result> <result name="error">/error1.jsp</result> </action> </package>五、动态结果
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>登录页面</title></head><body><form action="user/login.action" method="post"><table><tr><td>用户名:</td><!--这里的name要和提交的地址中声明的实体类.属性来用 --><td><input type="text" name="user.username" /></td></tr><tr><td>密码:</td><!--这里的name要和提交的地址中声明的实体类.属性来用 --><td><input type="password" name="user.password" /></td></tr><tr><td><input type="submit" value="登录" /></td><td><input type="reset" value="重置" /></td></tr></table></form></body></html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>管理员用户页面</title></head><body>管理员用户页面</body></html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>普通用户</title></head><body>普通用户页面</body></html>
User实体类
package com.pb.enity;public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
UserAction
package com.pb.web.action;import com.opensymphony.xwork2.ActionSupport;import com.pb.enity.User;public class UserAction extends ActionSupport { private static final long serialVersionUID = 1L; private User user; //下一个中转的Action private String nextDispose; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getNextDispose() { return nextDispose; } public void setNextDispose(String nextDispose) { this.nextDispose = nextDispose; } public String login(){ //用户是admin就转到adminAction if(user.getUsername().equals("admin") && user.getPassword().equals("admin")){ nextDispose="admin"; return SUCCESS; //用户是admin就转到commonAction }else if(user.getUsername().equals("common") && user.getPassword().equals("common")){ nextDispose="common"; return SUCCESS; //其它就跳转到登录页面 }else{ return INPUT; } } }
CommonAction
package com.pb.web.action;import com.opensymphony.xwork2.ActionSupport;public class CommonAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; @Override public String execute() throws Exception { return SUCCESS; }}
AdminAction
package com.pb.web.action;import com.opensymphony.xwork2.ActionSupport;public class AdminAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; @Override public String execute() throws Exception { // TODO Auto-generated method stub return SUCCESS; }}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <!-- Add packages here --> <package name="user" namespace="/user" extends="struts-default"> <action name="login" class="com.pb.web.action.UserAction" method="login"> <!-- 通过${}取出在UserAciton中定义的变量对应下面的action name --> <result name="success" type="redirectAction">${nextDispose}</result> <result name="input">/login.jsp</result> </action> <action name="admin" class="com.pb.web.action.AdminAction"> <result name="success">/admin.jsp</result> </action> <action name="common" class="com.pb.web.action.CommonAction"> <result name="success">/common.jsp</result> </action> </package></struts>
新闻热点
疑难解答