开春大礼《华为云技术精选集》大厂100+前沿技术实战分享!>>>
Simpleframework整合Struts2的简化实现
概念
Simpleframework整合Struts2配置文档,这里simple 整合Struts2,只是对Struts2做了业务功能的补充,在整合好的应用中,simple 提供内置的组件,比如核心组件,基本组件(包括只想使用simple 的ui,按照本文档的配置就可以满足你的需求),业务组件功能的使用。
注: 与已有业务系统整合步骤与整合struts2操作步骤类似。
下面是详细的配置文档:
第一步,在web.xml配置 simple与struts2的Filter。web.xml
<?xmlversion="1.0" encoding="UTF-8"?> <web-appxmlns: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" id="WebApp_ID" version="2.5"> <display-name> simpleframework </display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- simple 与struts 整合filter 配置顺序--> <filter> <filter-name>actionFilter</filter-name> <filter-class>net.simpleframework.web.page.PageFilter</filter-class> </filter> <filter-mapping> <filter-name>actionFilter</filter-name> <url-pattern>*. jsp </url-pattern> </filter-mapping> <filter-mapping> <filter-name>actionFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value> gbk </param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<listener> <listener-class>net.simpleframework.web.page.PageEventAdapter</listener-class> </listener> </web-app> |
第二步,准备struts与simple的必要的jar 包,下载工程中已经整合该例子的所有 jar 包,目录在WEB-INF\lib下。
第三步,编写Struts测试代码,查看测试效果
1)需要编写src 目录下编写如下代码:packagetest;
importcom.opensymphony.xwork2.ActionSupport;
publicclassHelloWorld extendsActionSupport {
@Override
publicString execute() throwsException {
setMessage("Struts is up and running ... hello world..");
returnSUCCESS;
}
privateString message;
publicvoidsetMessage(String message) {
this.message= message;
}
publicString getMessage() {
returnmessage;
}
privatestaticfinallongserialVersionUID= 289730245253011501L;
}
2)配置 struts.xml
<!DOCTYPEstruts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<packagename="test" extends="struts-default">
<actionname="hw" class="test.HelloWorld">
<result>/helloworld.jsp</result>
</action>
</package>
</struts>
例子
下载工程是一个完整的示范项目,运行index.jsp可以查看结果,里面包含源代码。
效果如下:
占个位