Struts2 详解

来源:互联网 发布:知乎2018校园招聘 编辑:程序博客网 时间:2024/06/03 15:55
Struts2 总结
A 第一讲

(1) Struts web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>  <filter>  <filter-name>struts2</filter-name>  <filter-class>  org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  </filter-class>  </filter>  <filter-mapping>  <filter-name>struts2</filter-name>  <url-pattern>/*</url-pattern>  </filter-mapping>  </web-app>
(2) Struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts><constant name="struts.devMode" value="true"/><!-- 打印更多错误信息 --><constant name="struts.i18n.encoding" value="utf-8"></constant><!-- 设置编码,2.1还没有处理这个bug --><constant name="struts.enable.DynamicMethodlnvocation" value="true"/><!-- 动态方法调用开关,感叹号调用 --><constant name="struts.ognl.allowStaticMethodAccess" value="true"/><!-- ognl调用静态方法 --><package name="sys" namespace="/sts" extends="struts-default"><!-- 默认action --><action name="hello"><result>/index.jsp</result></action><action name="dml" class="action.Action01"><result name="save">/save.jsp</result></action><action name="dml*" class="action.Action02" method="{1}"><!-- 动态传递action中的参数值 --><result name="update">/update.jsp?userName=${userName}</result><result name="delete">/delete.jsp</result></action><action name="action03" class="action.Action03"><result name="error">/error.jsp</result><result name="success">/success.jsp</result></action><action name="ognl" class="action.OgnlAction"><result name="ognl">/ongl.jsp</result></action></package></struts>    
(3) 用到的几个action
package action;public class Action01 {private String userName;public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String save(){return "save";}}
package action;public class Action02 {private String userName ;public String getUserName() {return "userName";}public void setUserName(String userName) {this.userName = userName;}public String delete() {return "delete";}public String update() {return "update";}}
package action;import com.opensymphony.xwork2.ActionSupport;public class Action03 extends ActionSupport{private String userName;public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}@Overridepublic String execute() throws Exception {if(userName==null||userName.equals("")){this.addFieldError("name", "name is error !");return "error";}else {return "success";}}}
package action;import javax.servlet.ServletRequest;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import bean.Cat;import bean.User;import com.opensymphony.xwork2.ActionSupport;public class OgnlAction{private String userName;private User user;private Cat cat;public Cat getCat() {return cat;}public void setCat(Cat cat) {this.cat = cat;}public User getUser() {return user;}public void setUser(User user) {this.user = user;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String execute()  {HttpServletRequest request = ServletActionContext.getRequest();request.getSession().setAttribute("userId", "2");return "ognl";}public String say(){return "action";}}
(4) bean
package bean;public class Cat {public static int id = 3;private String name;private Dog dog;public Dog getDog() {return dog;}public void setDog(Dog dog) {this.dog = dog;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Cat() {System.out.println("new cat !");}public String voice(){return "miaomiao";}public static String staticVoice(){return "wangwang";}}
package bean;public class Dog {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public Dog() {}}
package bean;public class User {private int id;public int getId() {return id;}public void setId(int id) {this.id = id;}public User() {}}
(5) index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@taglib prefix="s" uri="/struts-tags"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>   <a href="<%=basePath%>sts/dml!save">动态方法调用</a><br>   <a href="<%=basePath%>sts/dmlupdate">通配符action</a><br>   <a href="<%=basePath%>sts/dml2!update">通配符method</a><br>   <a href="<%=basePath%>sts/action03">error</a><br>   <a href="<%=basePath%>sts/ognl?userName=slm&user.id=1&cat.dog.name=alsj">ognl</a><br>   <s:debug></s:debug>  </body></html>
(6) ognl.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@taglib prefix="s" uri="/struts-tags"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>  ValueStatic:各种取值和方法调用<br/>  addFieldError: <s:property value="errors.name[0]"/><br>  userName=<s:property value="userName"/><br/>  user.id=<s:property value="user.id"/><br/>  dog.name=<s:property value="cat.dog.name"/><br/>  cat.voice 普通方法调用:<s:property value="cat.voice()"/><br/>  Action中普通方法调用:<s:property value="say()"/><br/>   类静态方法调用:<s:property value="@bean.Cat@staticVoice()"/><br/>   类静态属性访问:<s:property value="@bean.Cat@id"/><br/>    任意类 构造方法:<s:property value="bean.Cat.Cat()"/><br/>      调用Math类中的方法:<s:property value="@@max(2,3)"/><br/>  访问action对象:   <s:property value="[0]"/><br/>   <hr/>   非ValueStatic:各种取值和方法调用<br/>  session: <s:property value="#session.userId"/><br/>   <s:debug></s:debug>  </body></html>

B 第二讲 拦截器

(1) 拦截器类
package com.intercper;import org.apache.struts2.components.Reset;import com.action.loginaction;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;public class sysInscerpt implements Interceptor {public void destroy() {// TODO Auto-generated method stub}public void init() {System.out.println("自定义拦截器生成。。。");}public String intercept(ActionInvocation arg0) throws Exception {System.out.println("进入拦截器。。。");if (arg0.getAction().getClass() == loginaction.class) {return arg0.invoke();}ActionContext act = ActionContext.getContext();if (act.getSession().get("user") == null) {return "login";}String res = arg0.invoke();System.out.println(res);return res;}}
(2) 拦截器配置
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts><package name="sys" abstract="true" extends="struts-default"><interceptors><interceptor name="sysit1" class="com.intercper.sysInscerpt"></interceptor><interceptor-stack name="sysit"><interceptor-ref name="sysit1"></interceptor-ref><interceptor-ref name="defaultStack"></interceptor-ref></interceptor-stack></interceptors><default-interceptor-ref name="sysit"></default-interceptor-ref><global-results><result name="login">/index.jsp?log=0</result></global-results></package><include file="app.xml"></include></struts>    

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts><package name="app" namespace="/app1" extends="sys"><action name="loginaction_*" class="com.action.loginaction" method="{1}"><interceptor-ref name="sysit"></interceptor-ref><result name="true">/test.jsp</result></action><action name="test_*" class="com.action.testAction" method="{1}"></action><action name="va" class="com.action.ValiadateAction" ><result name="1">/v02.jsp</result></action></package></struts>    

C 两个项目的清单

0 0
原创粉丝点击