简介
Struts是基于MVC的WEB框架。是独立项目,不支持tomcat创建方式,使用动态web方式进行创建Struts项目。运行原理在于Struts中的filter拦截请求(web.xml进行配置),交由Struts处理(Struts.xml配置)。
demo创建步骤
- WEB-INF下创建web.xml配置filter
<web-app>
<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>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
- src创建struts.xml配置struts
<?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="basicstruts" extends="struts-default">
<!-- 访问index时服务端跳转index.jsp-->
<action name="index">
<result>index.jsp</result>
</action>
</package>
</struts>
显示数据到JSP
struts.xml配置
<!-- 访问showProduct的时候,调用ProductAction的show方法,根据返回值决定跳转到哪 -->
<action name="showProduct" class="Action.ProductAction" method="show">
<result name="aa">index.jsp</result>
</action>
JSP:
<%@page isELIgnored="false"%>
${product.name}
原理:会调用getProduct将返回值放在request.setAttribute中方便EL表达式调用
提交数据到action
- 指定如下表单
<html>
<form action="addAge">
<input type="text" name="product.age">
<br/>
<input type="submit" value="submit">
</form>
</html>
- 值定addAge提交是会进入ProducAction中调用add方法。在调用之前会将前端传过来的product.age生成product对象,并调用setAge方法指定属性。在通过Action的setProduct注入到Action
<action name="addAge" class="Action.ProductAction" method="add">
<result name="show">index.jsp</result>
</action>
中文编码
jsp中的中文同JSP章节。struts的解码方式设置
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
request和response
struts也可以访问servlet中的request和response
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.how2java.bean.Product;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
获取session
struts中的session有两个
- 传统servlet中的session
- struts自带的session,以map形式出现
public String add() {
Map m = ActionContext.getContext().getSession();
m.put("name", product.getName());
return "show";
}
拦截器
struts自身带有拦截器进行拦截处理,也可以创建自己的拦截器。
创建拦截器
package demo.interceptor;
import java.util.Date;
import action.ProductAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class DateInterceptor extends AbstractInterceptor {
public String intercept(ActionInvocation invocation) throws Exception {
// 将获取到的拦截器强制转化为ProductAction,并进行时间的注入
ProductAction action = (ProductAction)invocation.getAction();
action.setDate(new Date());
return invocation.invoke();
}
}
配置struts
配置struts使用声明拦截器并使用拦截器
// 声明拦截器
<interceptors>
<interceptor name="dateInterceptor" class="interceptor.DateInterceptor" />
</interceptors>
<action name="*Product*" class="action.ProductAction"
method="{1}">
//使用拦截器,使用拦截器会造成默认拦截器失效,第二个拦截器是指定默认拦截器
<interceptor-ref name="dateInterceptor" />
<interceptor-ref name="defaultStack" />
<result name="show">show.jsp</result>
<result name="list">list.jsp</result>
</action>
客户端跳转
struts是使用服务端跳转,如果需要使用客户端跳转,在result中添加type='redirect'
<action name="showProduct" class="action.ProductAction" method="show">
<result name="show">show.jsp</result>
</action>
客户端跳转传参
直接和正常query一样
<action name="addPageProduct" class="action.ProductAction" method="addPage">
<result name="addPage" type="redirect">addProduct.jsp?name=${name}</result>
</action>
不过在action中需要设置相应的属性
public String addPage(){
name = "default name";
return "addPage";
}
注解方式
需要先把xml中的配置取消,以确保注解能生效
demo:
package action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import bean.Product;
// 访问路径
@Namespace("/")
// 使用默认拦截器
@ParentPackage("struts-default")
// 预先定义多个拦截器
@Results({@Result(name="show", location="/show.jsp"),
@Result(name="home", location="/index.jsp")})
public class ProductAction {
private Product product;
// 访问showProduct时调用show方法
@Action("showProduct")
public String show() {
product = new Product();
product.setName("iphone7");
return "show";
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
}