目标:
1.strust的配置
2.动态方法的调用
3.struts传递参数
4.struts与tomcat的集成
1.strust的配置
在通过maven方式导入strust2依赖之后,(在pom.xml中引入strust和tomcat的),写在dependencies标签中
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.13</version>
</dependency>
在项目的web.xml中,配置strust的过滤器
代码:
<!-- struts2中央控制器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
再拷入三个配置文件
struts.xml:主要的配置文件,在这里面会引用其他三个配置文件,不一一介绍了
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!-- 引入了框架自身的默认配置 -->
<include file="struts-default.xml"></include>
<!-- 引入开发人员对框架的基本配置 -->
<include file="struts-base.xml"></include>
<!-- 项目涉及到的模块文件管理,类似之前的mvc.xml,不过这里可以分模块管理 -->
<include file="struts-sy.xml"></include>
</struts>
struts-base.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.i18n.reload" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="base" extends="struts-default" abstract="true">
<global-allowed-methods>regex:.*</global-allowed-methods>
</package>
</struts>
struts-sy.xml 默认文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="sy" extends="base" namespace="/sy">
</package>
</struts>
2.动态方法调用
目标,通过strust框架,实现页面跳转,及子控制器方法调用
a.先写自控制器,测试用的
package com.csf.web;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport{
public String test() {
System.out.println("testing...");
return "toIndex";
}
public String list() {
System.out.println("listing...");
return "toList";
}
}
b.配置到ttruts-sy.xml的package标签内
<package name="sy" extends="base" namespace="/sy">
<action name="/test_*" class="com.csf.web.TestAction" method="{1}">
<result name="toIndex">test.jsp</result>
<result name="toList">list.jsp</result>
</action>
</package>
原本只有<package>标签,在里面写<action>标签,注意,<package>标签代表不同的包路径
然后<action>标签的name属性,代表在前端的访问路径,class属性代表具体的子控制器,method={1},代表name属性的' * ',也就是方法名,(可以在name属性里写两个*,指引跳转的目标界面,不过不推荐,所以我省略了)
<result>标签:跳转的目标路径,name是方法的返回值,标签内容是目标路径
OK,现在先写一个主页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>strust动态方法调用</h3>
<a href="${pageContext.request.contextPath }/sy/test_test">点我</a>
<a href="${pageContext.request.contextPath }/sy/test_list">点我2</a>
</body>
</html>
注意看两个链接调用方法的方式,区别于之前mvc中方法的调用。
看效果:
点击第一个按钮,
控制台输出:
页面跳转:
说明strust的应用成功了,第二个按钮也是一样的,就不过多演示。
3、strust参数传递
也就是在子控制器中,接受前端参数,
回忆mvc,在mvc框架中,是通过实现ModelDriver,接口,建立对象,从而接收的
在strust中,有三种方式接收参数
a.实现ModelDriver接口
b.在子控制器中写属性,并且提供set方法
c.通过对象属性传参
ok,先将子控制器进行修改
package com.csf.web;
import com.csf.entity.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class TestAction extends ActionSupport implements ModelDriven<User>{
private User user =new User();
private String uname;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
private User user2;
public User getUser2() {
return user2;
}
public void setUser2(User user2) {
this.user2 = user2;
}
public String test() {
System.out.println("testing...");
return "toIndex";
}
public String list() {
System.out.println("listing...");
return "toList";
}
public String a() {
System.out.println(user);
return "toIndex";
}
public String b() {
System.out.println(mname);
return "toIndex";
}
public String c() {
System.out.println(user2);
return "toIndex";
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
三种方式分别用a,b,c三个方法来表现。
前端页面加入:
<h3>strust参数传递</h3>
<a href="${pageContext.request.contextPath }/sy/test_a?id=1&name=aa">ModelDriver接口传递参数</a>
<a href="${pageContext.request.contextPath }/sy/test_b?mname=bb">set方法传递参数</a>
<a href="${pageContext.request.contextPath }/sy/test_c?user2.id=2&user2.name=cc">对象传递参数</a>
依次点击三个按钮:
控制台如果有输出对应的值,则代表成功。
ok,成功了,现在再试一下如果不提供set方法 ,会出现一下报错,并且对应的属性为空
4.与tomcat的集成
因为在子控制器的方法中,与之前自定义的mvc框架的方法不同,这里没有请求和相应对象,而要得到这两个对象有点麻烦
分别要实现ServletRequestAware接口得到请求对象,实现ServletResponseAware接口得到相应对象,比较麻烦所以我们将子控制器进行抽取升级,得到BaseAction,将一些重复,通用的步骤封装进去
BaseAction:
package com.util;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
* 每一个开发的子控制器要用的属性都定义在通用的action中。
* @author Administrator
*
*/
public abstract class BaseAction<T> extends ActionSupport implements ServletRequestAware, ServletResponseAware,ModelDriven<T>{
/**
* 为了传值使用
*/
protected HttpServletResponse response;
protected HttpServletRequest request;
protected HttpSession session;
protected ServletContext application;
/**
* 为了配置跳转页面所用
*/
protected final static String SUCCESS = "success";
protected final static String FAIL = "fail";
protected final static String LIST = "list";
protected final static String TOEDIT = "toEdit";
protected final static String TOLIST = "toList";
/**
* 具体传值字段 后端向jsp页面传值所用字段
*/
protected Object result;
protected Object msg;
protected int code;
public Object getResult() {
return result;
}
public Object getMsg() {
return msg;
}
public int getCode() {
return code;
}
@Override
public void setServletResponse(HttpServletResponse arg0) {
this.response = arg0;
}
@Override
public void setServletRequest(HttpServletRequest arg0) {
this.request = arg0;
this.session = arg0.getSession();
this.application = arg0.getServletContext();
}
}
BaseAction,定义了一些返回值常量,避免因编码产生的错误。并且实现了ServletRequestAware,ServletResponseAware,ModelDrive接口,方便使用。
5.总结
1.strust的配置
导入strust依赖,和相关的三个配置文件,在web.xml中配置中央过滤器,每一个子控制器需要在strust-sy.xml中进行配置
2.动态方法的调用
区别于之前的自定义mvc框架,strust中,方法调用只要在访问路径后加下划线和方法名就行
例如:"${pageContext.request.contextPath }/sy/test_b?mname=bb
3.struts传递参数
三种传参方式,核心要素是给属性提供set方法
4.struts与tomcat的集成
给子控制器写一个爹,一劳永逸。