Struts的附加功能
[1] 拦截器:
建立一个拦截器的类MyInterceptor
public class MyInterceptor1 implements Interceptor {
public void init() {// 覆盖Interceptor接口中的init函数
System.out.println("拦截器已经被加载");
}
public void destroy() {// 覆盖Interceptor接口中的destroy函数
System.out.println("destroy");
}
/* 覆盖Interceptor接口中的intercept函数 */
public String intercept(ActionInvocation invocation) throws
Exception {
System.out.println("调用intercept方法");
/* invocation.invoke()方法检查是否还有拦截器 有的话继续调
用余下的拦截器 没有了则执行action的业务逻辑 */
String result = invocation.invoke();//放行
System.out.println("啧啧啧啧啧啧");
return result;
}
}
Struts2配置文件,拦截器的映射
<!-- 定义拦截器 -->
<interceptors>
<interceptor
name="myInterceptor"class="com.interceptor.MyInterceptor1"/>
</interceptors>
<!-- 配置action -->
<action name="testInterceptor" class="com.action.InterceptorTest">
<result name="success">/jsps/success.jsp</result>
<!-- 将声明好的拦截器插入action中 -->
<interceptor-ref name="myInterceptor" />
<!-- 缺省拦截器栈<default-interceptor-ref >定义,如果没有定义自己的
拦截器;则所有Action都会执行的缺省拦截器栈拦截器,并按照循序从上到 下
执行。反之;需要手动添加;
否则缺省拦截器栈不起作用-->
<interceptor-ref name="defaultStack" />
</action>
[2]转换器
类型转换的流程:
1、用户进行请求,根据请求名在struts.xml中寻找Action。
2、在Action中,根据请求域中的名字去寻找对应的set方法。找到后在赋值之前会检查这个属性有没有自定义的类型转换。没有的话,按照默认进行转换;如果某个属性已经定义好了类型转换,则会去检查在Action同一目录下的 action文件名-conversion.properties 文件。
3、从文件中找到要转换的属性及其转换类。
4、然后进入转换类中,在此类中判断转换的方向。我们是先从用户请求开始的,所以这时先进入从字符串到类的转换。返回转换后的对象。流程返回Action。
5、将返回的对象赋值给Action中的属性,执行Action中的execute()。
6、执行完execute()方法,根据struts.xml的配置转向页面。
7、在jsp中显示内容时,根据页面中的属性名去调用相应的get方法,以便输出。
8、在调用get方法之前,会检查有没有此属性的自定义类型转换。如果有,再次跳转到转换类当中。
9、在转换类中再次判断转换方向,进入由类到字符串的转换,完成转换后返回字符串。
10、将返回的值直接带出到要展示的页面当中去显示。
编写转换类PointConverter.java
public class PointConverter extends DefaultTypeConverter {//继承DefaultTypeConverter
public Object convertValue(Map context, Object value, Class toType) {
/* 参数所代表的意思
* Map context页面上下文,
* Object value 是要进行类型转换的值。
* toType:来指定向哪一种类型转换,即是向类转换还是向客户端转换
*/
if (Point.class == toType) { // 由客户端向类转换
Point point = new Point();// new这个类
String[] str = (String[]) value;
String[] values = str[0].split(",");
// 下面部分代码就是进行转换处理
point.setX(Integer.parseInt(values[0]));
point.setY(Integer.parseInt(values[1]));
return point;
}
if (String.class == toType) {// 说明由类转换成String
Point point = (Point) value;// 将类转成String的代码处理
return point.toString();
}
return null;
}
}
Point.java
public class Point {
private int x, y;
public String toString() {
return "(" + x + "," + y + ")";
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
ConverterAction.java
public class ConverterAction extends ActionSupport{
Point point;
public String execute() throws Exception {
System.out.println(point.toString());
return SUCCESS;
}
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
}
ConverterAction-conversion.properties
point=com.converter.PointConverter
[1] 拦截器:
建立一个拦截器的类MyInterceptor
public class MyInterceptor1 implements Interceptor {
public void init() {// 覆盖Interceptor接口中的init函数
System.out.println("拦截器已经被加载");
}
public void destroy() {// 覆盖Interceptor接口中的destroy函数
System.out.println("destroy");
}
/* 覆盖Interceptor接口中的intercept函数 */
public String intercept(ActionInvocation invocation) throws
Exception {
System.out.println("调用intercept方法");
/* invocation.invoke()方法检查是否还有拦截器 有的话继续调
用余下的拦截器 没有了则执行action的业务逻辑 */
String result = invocation.invoke();//放行
System.out.println("啧啧啧啧啧啧");
return result;
}
}
Struts2配置文件,拦截器的映射
<!-- 定义拦截器 -->
<interceptors>
<interceptor
name="myInterceptor"class="com.interceptor.MyInterceptor1"/>
</interceptors>
<!-- 配置action -->
<action name="testInterceptor" class="com.action.InterceptorTest">
<result name="success">/jsps/success.jsp</result>
<!-- 将声明好的拦截器插入action中 -->
<interceptor-ref name="myInterceptor" />
<!-- 缺省拦截器栈<default-interceptor-ref >定义,如果没有定义自己的
拦截器;则所有Action都会执行的缺省拦截器栈拦截器,并按照循序从上到 下
执行。反之;需要手动添加;
否则缺省拦截器栈不起作用-->
<interceptor-ref name="defaultStack" />
</action>
[2]转换器
类型转换的流程:
1、用户进行请求,根据请求名在struts.xml中寻找Action。
2、在Action中,根据请求域中的名字去寻找对应的set方法。找到后在赋值之前会检查这个属性有没有自定义的类型转换。没有的话,按照默认进行转换;如果某个属性已经定义好了类型转换,则会去检查在Action同一目录下的 action文件名-conversion.properties 文件。
3、从文件中找到要转换的属性及其转换类。
4、然后进入转换类中,在此类中判断转换的方向。我们是先从用户请求开始的,所以这时先进入从字符串到类的转换。返回转换后的对象。流程返回Action。
5、将返回的对象赋值给Action中的属性,执行Action中的execute()。
6、执行完execute()方法,根据struts.xml的配置转向页面。
7、在jsp中显示内容时,根据页面中的属性名去调用相应的get方法,以便输出。
8、在调用get方法之前,会检查有没有此属性的自定义类型转换。如果有,再次跳转到转换类当中。
9、在转换类中再次判断转换方向,进入由类到字符串的转换,完成转换后返回字符串。
10、将返回的值直接带出到要展示的页面当中去显示。
编写转换类PointConverter.java
public class PointConverter extends DefaultTypeConverter {//继承DefaultTypeConverter
public Object convertValue(Map context, Object value, Class toType) {
/* 参数所代表的意思
* Map context页面上下文,
* Object value 是要进行类型转换的值。
* toType:来指定向哪一种类型转换,即是向类转换还是向客户端转换
*/
if (Point.class == toType) { // 由客户端向类转换
Point point = new Point();// new这个类
String[] str = (String[]) value;
String[] values = str[0].split(",");
// 下面部分代码就是进行转换处理
point.setX(Integer.parseInt(values[0]));
point.setY(Integer.parseInt(values[1]));
return point;
}
if (String.class == toType) {// 说明由类转换成String
Point point = (Point) value;// 将类转成String的代码处理
return point.toString();
}
return null;
}
}
Point.java
public class Point {
private int x, y;
public String toString() {
return "(" + x + "," + y + ")";
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
ConverterAction.java
public class ConverterAction extends ActionSupport{
Point point;
public String execute() throws Exception {
System.out.println(point.toString());
return SUCCESS;
}
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
}
ConverterAction-conversion.properties
point=com.converter.PointConverter