Stripes最基本的实现是类似于Struts的请求,但没有Struts那么配置繁琐。下面就来看看怎样实现一个web应用的Action访问操作。本篇博文为设计Stripes的三种访问方式:普通Action的映射访问、类Servlet访问方式、Rest变参访问方式。前几天还没将Stripes应用示例打通,今天又本着不解决问题死不罢休的精神继续去弄,终于搞明白了。
1、编写Stripes的web.xml配置文件
该文件定义了第三方的工具包、过滤器类、以及映射关系和访问格式;在此配置文件中有两种Stripes的配置(特定模板的Action过滤配置和动态URL路径配置),它们互不排斥,相互包容,方便了用户不同业务需求下的访问模式。其配置内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!-- 如果没使用到,此内容可以不添加 -->
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>StripesResources</param-value>
</context-param>
<!-- 基本ActionBean访问过滤配置 -->
<filter>
<display-name>Stripes Filter</display-name>
<filter-name>StripesFilter</filter-name>
<filter-class>net.sourceforge.stripes.controller.StripesFilter</filter-class>
<init-param>
<param-name>ActionResolver.Packages</param-name>
<param-value>com.boonya.stripes.actions</param-value>
</init-param>
<init-param>
<param-name>Interceptor.Classes</param-name>
<param-value>
net.sourceforge.stripes.integration.spring.SpringInterceptor
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>StripesFilter</filter-name>
<servlet-name>StripesDispatcher</servlet-name>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<servlet>
<servlet-name>StripesDispatcher</servlet-name>
<servlet-class>net.sourceforge.stripes.controller.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>StripesDispatcher</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 动态配置隐射过滤器 -->
<filter>
<description>Dynamically maps URLs to ActionBeans.</description>
<display-name>Stripes Dynamic Mapping Filter</display-name>
<filter-name>DynamicMappingFilter</filter-name>
<filter-class>
net.sourceforge.stripes.controller.DynamicMappingFilter
</filter-class>
<init-param>
<param-name>ActionResolver.Packages</param-name>
<param-value>com.boonya.stripes.actions</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>DynamicMappingFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<!-- 欢迎界面 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2、导入需要的jar包到项目WEB-INF的lib目录下
此处需要加入的jar包,由于Stripes实现的时候用到了springframework的jar包,所以需要在项目中添加,否则在访问页面时会有异常产生。示例项目目录结构如下图:
注:上面所示的Jar有个别不需要,如果需要使用Spring整合建议保留。
3、编写抽象的ActionBean,AbstractActionBean
因为在每个ActionBean中都使用到了上下文对象设置,这部分属于公共行为,所以将其抽象出来。
package com.boonya.stripes.actions;
import java.io.Serializable;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.SimpleMessage;
public abstract class AbstractActionBean implements ActionBean, Serializable {
private static final long serialVersionUID = -1767714708233127983L;
protected static final String ERROR = "/WEB-INF/Error.jsp";
protected transient ActionBeanContext context;
protected void setMessage(String value) {
context.getMessages().add(new SimpleMessage(value));
}
public ActionBeanContext getContext() {
return context;
}
public void setContext(ActionBeanContext context
补充:综合编程 , 其他综合 ,