因为工作临时需要,自己接触并学习使用了Struts框架。下面记录下自己在Struts2框架下的代码开发流程
1、配置web.xml文件
web.xml配置文件是一种J2EE配置文件,决定servlet容器的HTTP元素需求如何进行处理。它严格来说不是一个Struts2 配置文件,但它是Struts2 运作所需要进行配置的文件。
<?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" 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>itcastActiviti</display-name>
<filter>
<filter-name>encodingFilter</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>
<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>*.action</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
注意,我们将Struts2 过滤器映射到 /*.action ,这意味着只有*.action的url才会被Struts过滤器解析。
2、配置struts.xml文件
struts.xml文件包含有随着Actions的开发 将要修改的配置信息。它可用于覆盖应用程序的默认设置,例如:struts.devMode=false 以及其他定义为属性文件的设置。这个文件可在resource文件夹下创建。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 配置模式为开发模式,自动加载struts.xml和显示错误提示信息 -->
<constant name="struts.devMode" value="false" />
<!-- 设置页面主题为简单主题,去掉struts2开发提供的样式 -->
<constant name="struts.ui.theme" value="simple" />
<package name="default" namespace="/" extends="struts-default">
<!-- 全局结果视图 -->
<global-results>
<result name="login" type="redirect">
login.jsp
</result>
</global-results>
<action name="meetingAction_*" class="meetingAction" method="{1}">
<result name="getlist">WEB-INF/views/meeting/list.jsp</result>
<result name="list" type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
<result name="input">WEB-INF/views/meeting/input.jsp</result>
<result name="update" type="redirectAction">
<param name="actionName">meetingAction_getlist.action</param>
</result>
<result name="save" type="redirectAction">
<param name="actionName">meetingAction_getlist.action</param>
</result>
<result name="delete" type="redirectAction">
<param name="actionName">meetingAction_getlist.action</param>
</result>
<result name="onstafflist">WEB-INF/views/meeting/onstafflist.jsp</result>
<result name="checkApplyLeave" type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>
<action name="dutyScheduleAction_*" class="dutyScheduleAction" method="{1}">
<!-- <result name="getlist">WEB-INF/views/meeting/calendar.jsp</result> -->
<result name="getlist">WEB-INF/views/dutyschedule/list.jsp</result>
<result name="list" type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
<result name="getadminlist">WEB-INF/views/meeting/calendar_admin.jsp</result>
<result name="show" type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
<result name="showevents" type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
<result name="save" type="redirectAction">
<param name="actionName">dutyScheduleAction_getlist.action</param>
</result>
<result name="update" type="redirectAction">
<param name="actionName">dutyScheduleAction_getlist.action</param>
</result>
<result name="delete" type="redirectAction">
<param name="actionName">dutyScheduleAction_getlist.action</param>
</result>
<result name="validAvailable" type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>
<action name="loginAction_*" class="loginAction" method="{1}">
<!-- <result name="success">WEB-INF/views/main.jsp</result> -->
<result name="success">bgpt/main_bgpt.jsp</result>
<!-- <result name="success" type="redirectAction">
<param name="actionName">holidayApplyAction_input.action</param>
</result> -->
<result name="top">WEB-INF/views/top.jsp</result>
<result name="left">WEB-INF/views/left.jsp</result>
<result name="welcome">WEB-INF/views/welcome.jsp</result>
</action>
</package>
</struts>
补充说明:struts.xml文件实现接口返回信息和页面关联的一个配置文件
3、其他配置,如数据库配置、activiti引擎配置等
4、开始编写代码,案例如下:
package cn.china.xxx;
public class loginAction{
private String name;
public String success() throws Exception {
return "success";
}
}