struts2:属于web层,代替servlet技术
strut2:接收并处理请求.
struts2:框架封装了很多web层开发中常见的功能,我们使用这些功能不需要我们来增加代码实现了,只需要配置下就直接使用.
struts2:提高web层开发效率
核心:
intercepter 拦截器 :请求经过action之前先经过拦截器
- 拦截器是用来封装功能,
- 体现AOP思想
- 可插拔式的灵活设计
- 早期版本默认经过18个拦截器,当前新版本默认经过20个拦截器
AOP思想:面向切面编程
纵向重复,横向抽取
核心配置文件:struts.xml
基本配置
package元素:封装Action配置,方便项目的模块开发
name属性:给包起个名称,不能重复
namespace属性:命名空间,指定Action资源名前的访问路径
不写该属性,相当于填写"/",该属性值可以重复
extends属性:继承另一个包,会将配置信息继承,必须继承struts-default.
action元素:配置action对象
name属性:指定访问action时填写的资源名称
class属性:填写Action完整类名,用于struts2框架创建action对象
method属性:定位类中处理请求的方法,填写方法名.
result元素:定义结果
name属性:对应Action中方法的返回值
type属性:指定交给那个结果处理器来处理
转发(默认值) dispatcher
重定向 redirect
标签体:指定跳转的资源路径,相对目录WebContent
<package name="hello" namespace="/hello" extends="struts-default">
<action name="HelloAction" class="cn.it.test.HelloAction" method="hello">
<result name="success" type="dispatcher">/hello.jsp</result>
</action>
</package>
常量配置
<!-- i18n:国际化.解决post提交乱码 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!-- 指定action访问后缀 .action或空 -->
<constant name="struts.action.extension" value="action,,"></constant>
<!-- struts2是否启用开发者模式 1.热加载 2.更多错误信息输出 -->
<constant name="struts.devMode" value="true"></constant>
<!-- 动态方法调用常量 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
结果跳转
转发
<result name="success" type="dispatcher">/hello.jsp</result>
重定向
<result name="success" type="redirect">/hello.jsp</result>
转发到Action
<result name="success" type="chain">
<param name="namespace">/</param>
<param name="actionName">Demo2Action</param>
</result>
重定向到Action
<result name="success" type="redirectAction">
<param name="namespace">/</param>
<param name="actionName">Demo3Action</param>
</result>
struts2获取servletAPI
public class Demo5Action extends ActionSupport{
public String api(){
Map<String, Object> session = ActionContext.getContext().getSession();
Map<String, Object> application = ActionContext.getContext().getApplication();
// Map<String, Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
ActionContext.getContext().put("name", "request");
session.put("name", "session");
application.put("name", "application");
return SUCCESS;
}
}
action 中方法返回类型必须为String 且无参。
struts2获取参数
属性驱动
public class Demo8Action extends ActionSupport{
private String name;
private Integer age;
private Date birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String form(){
System.out.println(name);
System.out.println(age);
System.out.println(birthday);
return SUCCESS;
}
}
//对象驱动
public class Demo9Action extends ActionSupport{
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String form(){
System.out.println(user);
return SUCCESS;
}
}
//模型驱动(常用)
public class Demo10Action extends ActionSupport implements ModelDriven<User>{
private User user = new User();
public String form(){
System.out.println(user);
return SUCCESS;
}
@Override
public User getModel() {
return user;
}
}
//集合类型封装
public class Demo11Action extends ActionSupport{
private List<String> list;
private Map<String,String> map;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public String form(){
System.out.println(list+" "+map);
return SUCCESS;
}
}
配置全局异常处理
<global-exception-mappings>
<exception-mapping result="error"
exception="java.lang.RuntimeException"></exception-mapping>
</global-exception-mappings>
<global-results>
<result name="toLogin" type="redirect">/login.jsp</result>
</global-results>
<action name="UserAction_*" class="lx.action.UserAction"
method="{1}">
<result name="toHome" type="redirect">/index.htm</result>
<result name="error" type="redirect">/login.jsp</result>
</action>
自定义拦截器栈
<!-- 1注册拦截器 -->
<interceptors>
<interceptor name="myinter"
class="lx.action.a_intercepter.Myintercepter3"></interceptor>
<!-- 2注册拦截器栈 -->
<interceptor-stack name="mystack">
<!-- 自定义拦截器 -->
<interceptor-ref name="myinter">
<!-- 指定哪些方法不拦截 -->
<!-- <param name="excludeMethods">add,update</param> -->
<!-- 指定哪些方法需要拦截 -->
<param name="includeMethods">add</param>
</interceptor-ref>
<!-- 默认拦截器 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 3指定默认拦截器 -->
<default-interceptor-ref name="mystack"></default-interceptor-ref>
loginInterceptor
public class LoginIntercepter extends MethodFilterInterceptor{
@Override
protected String doIntercept(ActionInvocation arg0) throws Exception {
//获得session
Map<String, Object> session = ActionContext.getContext().getSession();
//获得session登陆标志
Object object = session.get("user");
//判断登陆标志是否存在
if(object == null){
//不存在,重定向到登陆界面
return "toLogin";
}else{
//存在,放行
return arg0.invoke();
//不经过拦截器直接返回result
// return "success";
}
}
}