因爲工作臨時需要,自己接觸並學習使用了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";
}
}