struts框架设计
司空赞
2023-12-01
/***************************************************搭建一个最基本的struts框架应用*****************************/
配置Struts2框架的开发环境配置步骤如下:
1、导入struts2所需要的jar包
2、配置web.xml文件
3、编写action类
4、配置struts.xml文件。
(1)添加jar包:
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang-2.5.jar
commons-logging-1.1.1.jar
freemarker-2.3.16.jar
struts-el-1.3.10.jar
ognl-3.0.1.jar
struts2-core-2.2.3.jar
xwork-2.0.4.jar
(2)配置web.xml文件:
<?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"
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>SpringWebTest</display-name>
<!--以上是web.xml文件的头信息 不可改变-->
<!-- 过滤器,统一编码格式 -->
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.msp.util.FilterDemo</filter-class>
<init-param>
<param-name>encode</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 过滤器结束 -->
<!-- 也可以应用如下配置 不过要用到Spring的jar包 开始-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 也可以应用如下配置 不过要用到Spring的jar包 结束-->
<!-- 配置 struts2.0 过滤转发器 -->
<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>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 过滤转发器结束 -->
<!--定时器配置 开始-->
<listener>
<listener-class>com.msp.util.MyTimerListener</listener-class>
</listener>
<!--定时器配置结束-->
<!--session过期的问题配置 开始-->
<session-config>
<session-timeout>3000</session-timeout>
</session-config>
<!--session过期的问题配置 结束-->
解决工程编码的问题,统一编码格式UTF-8
/**
* 编码格式过滤器
* com.msp.util.FilterDemo的编写
*
*/
public class FilterDemo implements Filter{
private FilterConfig filterConfig;
private String encoding;
public void destroy() {
filterConfig=null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if(encoding!=null){
request.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}
}
public void init(FilterConfig config) throws ServletException {
filterConfig=config;
encoding=filterConfig.getInitParameter("encode");
}
}
/**
*
* 定时器的配置 一般都要实现ServletContextListener接口
* com.msp.util.MyTimerListener
*
/
public class MyTimerListener implements ServletContextListener {
private BugXmlTimer mytimer = new BugXmlTimer ();
public void contextInitialized(ServletContextEvent event) {
mytimer.timerStart();
}
public void contextDestroyed(ServletContextEvent event) {
mytimer.timerStop();
}
}
(3)编写action类
新建一个pojo类,继承 ActionSupport 复写其中的execute方法
public class TestAction extends ActionSupport{
public String loginAction() throws Exception {
//TODO登录操作
return SUCCESS;
}
public String loginOut() throws Exception {
//TODO退出操作
return SUCCESS;
}
}
4)配置struts.xml文件:注意头文件中的数字2.0,它属于版本号
新建 struts.xml 头文件如下:
<?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>
<constant name="struts.i18n.encoding" value="utf-8" />//国际化支持
<package name="struts2" namespace="/" extends="struts-default">
<!--拦截器配置开始-->
<interceptors>
<interceptor name="loginedCheck" class="com.msp.interceptor.LoginedCheckInterceptor"/>
<interceptor-stack name="mystack">
<interceptor-ref name="loginedCheck" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<!--拦截器配置结束-->
<!--Action节点配置开始-->
<action name="user_*" class="com.msp.struts.UserAction" method="{1}">
<result name="success">jsp/user/User_list.jsp</result>
<result name="add">jsp/user/User_add.jsp</result>
<result name="update">jsp/user/User_update.jsp</result>
<result name="error">jsp/login.jsp</result>
<result name="list" type="redirect">user_getAllUserlist</result>
<interceptor-ref name="mystack" />//拦截器栈的引用
</action>
<!--Action节点配置结束-->
</package>
</struts>