Struts2框架,执行流程
1:request访问
2:核心过滤器拦截
3:Interceptor拦截器拦截
4:调用指定Action
5:响应数据
Struts2配置文件
在web.xml文件中加入struts2的核心过滤器
<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>
简单的Struts2配置文件
<struts>
<package name="hello" namespace="/" extends="struts-default" >
<action name="helloWorld" class="cn.itcast.action.HelloWorldAction" >
<result>/success.jsp</result>
</action>
</package>
</struts>
常量配置
1: 在struts.xml配置,<constan>
例:
<struts>
<constant name="struts.il8n.encoding" value="UTF-8" />
</struts>
2:在struts.xml同目录下的 struts.properties 配置
例:
struts.action.extension=action,, //设置action的请求扩展名为action 或者没有扩展名
3:在web.xml 配置
例:
<Filter>
核心过滤器
<init-param> struts.il8n.encoding </init-param>
</Filter>
Action
<action name="" class="" method=""> //class="" 默认com.opensymphony.xwork2.ActionSupport类
//method="" 默认 execute()
<result name=" " >/xxx.jsp </result> //name="" 默认success
</action>
/* 通配符 */
<action name="*_*" class="cn.itcast.action.{1}" method="{2}"> //{1}:表示第一个通配符
//{2}:表示而二哥通配符
<result> {2}.jsp </result> //表示第二个通配符作为名字
</action>
Action访问Servlet API
通过ActionContext访问Servlet API
Map<String,Object> ActionContext.getAppication() //JPS对象的application
Map<String,Object> ActionContext.getSessiion() // session
ActionContext ActionContext.getContext() // request 即自己本身
Action处理请求参数
1: 属性驱动
class UserAction extends ActionSupport{
private User user; //JavaBean对象
getter() //略
setrer() //略
public String execute()throws Exception{
//直接调用即可
}
}
/*
jsp文件<form>表单的name属性需要设置成 name="user.name"
*/
2:模型驱动
class UserAction extends ActionSupport{
private User user=new User();
public User getModel(){ //模型方法
return user;
}
public String execute()throws Exception{
//直接调用即可
}
}
/*
jsp文件<form>表单的name属性需要设置成 name="name"即可不需要加前缀user
*/