[size=xx-large][color=orange][b]Struts2学习笔记[/b][/color][/size]
[color=red][b][size=medium]1.官网下载包[/size][/b][/color]
[color=red][b][size=medium]2.struts.xml尖括号提示:[/size][/b][/color]
[color=red][b][size=medium]3.工作流程图[/size][/b][/color]
[img]http://dl.iteye.com/upload/picture/pic/126749/d654611e-e1b7-3c8c-8999-956e1cbd7878.jpg[/img]
[color=red][b][size=medium]4.常量配置[/size][/b][/color]
[color=red][b][size=medium]5.jsp引入的标签s[/size][/b][/color]
[color=red][b][size=medium]5.struts.xml以及其他XML配置[/size][/b][/color]
[color=red][b][size=medium]5.struts.xml配置前台UI标签样式[/size][/b][/color]
[color=red][b][size=medium]5.拦截器执行过程[/size][/b][/color]
[img]http://dl.iteye.com/upload/picture/pic/126757/25622056-628f-3fa6-8b16-773d1f99a3b3.jpg[/img]
[color=red][b][size=medium]5.[/size][/b][/color]
[color=red][b][size=medium]5.[/size][/b][/color]
[color=red][b][size=medium]5.[/size][/b][/color]
[color=red][b][size=medium]5.[/size][/b][/color]
[color=red][b][size=medium]5.[/size][/b][/color]
[color=red][b][size=medium]1.官网下载包[/size][/b][/color]
URL:http://struts.apache.org/
解压:/apps/struts2-blank.war
拷贝:struts.xml+web.xml+jar
[color=red][b][size=medium]2.struts.xml尖括号提示:[/size][/b][/color]
a.解压struts2-core-2.3.15.1.jar
b.windows--preferences--catalog--add:
解压文件中的dtd
URL
http://struts.apache.org/dtds/struts-2.3.dtd
[color=red][b][size=medium]3.工作流程图[/size][/b][/color]
[img]http://dl.iteye.com/upload/picture/pic/126749/d654611e-e1b7-3c8c-8999-956e1cbd7878.jpg[/img]
[color=red][b][size=medium]4.常量配置[/size][/b][/color]
struts.xml配置:
<constant name="struts.i18n.encoding" value="UTF-8"/>
该常量会覆盖struts2-core-2.3.15.jar包中/org.apache.struts2.default.properties中的值
[color=red][b][size=medium]5.jsp引入的标签s[/size][/b][/color]
<%@ taglib prefix="s" uri="/struts-tags" %>
这个uri对应该struts2-core-2.3.15.jar包中/META-INF/struts-tags.tld
该文件中:
<display-name>Struts Tags</display-name>
<tlib-version>2.3</tlib-version>
<short-name>s</short-name>
<uri>/struts-tags</uri>
处理类:<tag-class>org.apache.struts2.views.jsp.ActionTag</tag-class>
这个uri和此处的uri对应
[color=red][b][size=medium]5.struts.xml以及其他XML配置[/size][/b][/color]
<?xml version="1.0" encoding="UTF-8" ?>
1.struts.xml:
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- true:开发模式,修改XML配置文件不用重启服务 -->
<constant name="struts.devMode" value="false" />
<constant name="struts.i18n.encoding" value="UTF-8"/>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<package name="default" namespace="/default" extends="struts-default">
<!-- 公共的跳转页面,所有Action都可以调用该result -->
<global-results>
<result name="error">/views/error.jsp</result>
</global-results>
<!-- 异常错误处理页面 -->
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
</package>
<!-- xml文件列表 -->
<include file="/default.xml"/>
<include file="/home.xml"/>
<include file="/userInfo.xml"/>
</struts>
extends="struts-default":继承了struts2-core-2.3.15.jar包中struts-default.xml的:
result结果集
Interceptor拦截器等
2.userInfo.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="userInfoPackage" namespace="/" extends="default">
<action name="userInfo_*" class="com.momo.action.UserInfoAction" method="{1}">
<result name="success">/views/userInfo/userInfo_{1}_success.jsp</result>
<result name="fail">/views/userInfo/userInfo_{1}_fail.jsp</result>
</action>
<!-- 服务其跳转:页面 -->
<action name="dispatcher">
<result type="dispatcher">/views/userInfo/dispatcher.jsp</result>
</action>
<!-- 客户端跳转:页面 -->
<action name="redirect">
<result type="redirect">/views/userInfo/redirect.jsp</result>
</action>
<!-- 服务器跳转:Action方法 -->
<action name="chain">
<result type="chain">
<!-- 指定要跳转的Action位置 -->
<param name="namespace">/</param>
<param name="actionName">dispatcher</param>
</result>
</action>
<!-- 客户端跳转:Action方法 -->
<action name="redirectAction">
<result type="redirectAction">redirect</result>
</action>
</package>
</struts>
extends="default":可以共用namespace="default"目录中的结果集result
[color=red][b][size=medium]5.struts.xml配置前台UI标签样式[/size][/b][/color]
struts.xml:
<constant name="struts.ui.theme" value="simple"/><!-- UI标签类型(ajax,simple,xhtml,css_xhtml) -->
jsp:
<s:form>
<s:fielderror/>
</s:form>
html源码样式会采用最简单的,支持本地覆盖struts2-core-2.3.15.jar包中template中的模板也支持自定义模板
[color=red][b][size=medium]5.拦截器执行过程[/size][/b][/color]
[img]http://dl.iteye.com/upload/picture/pic/126757/25622056-628f-3fa6-8b16-773d1f99a3b3.jpg[/img]
模拟原理如下:
Main.java
ActionInvocation.java
Interceptor.java
FirstInterceptor.java
SecondInterceptor.java
Action.java
public class Main {
public static void main(String[] args) {
new ActionInvocation().invoke();
}
}
public class ActionInvocation {
List<Interceptor> interceptors = new ArrayList<Interceptor>();
int index = -1;
Action a = new Action();
public ActionInvocation() {
this.interceptors.add(new FirstInterceptor());
this.interceptors.add(new SecondInterceptor());
}
public void invoke() {
index ++;
if(index >= this.interceptors.size()) {
a.execute();
}else {
this.interceptors.get(index).intercept(this);
}
}
}
public interface Interceptor {
public void intercept(ActionInvocation invocation) ;
}
public class FirstInterceptor implements Interceptor {
public void intercept(ActionInvocation invocation) {
System.out.println(1);
invocation.invoke();
System.out.println(-1);
}
}
public class SecondInterceptor implements Interceptor {
public void intercept(ActionInvocation invocation) {
System.out.println(2);
invocation.invoke();
System.out.println(-2);
}
}
public class Action {
public void execute() {
System.out.println("execute!");
}
}
[color=red][b][size=medium]5.[/size][/b][/color]
[color=red][b][size=medium]5.[/size][/b][/color]
[color=red][b][size=medium]5.[/size][/b][/color]
[color=red][b][size=medium]5.[/size][/b][/color]
[color=red][b][size=medium]5.[/size][/b][/color]