搭建Struts2环境
1)、找到开发struts应用所使用的jar包
2)、编写struts2的配置文件
3)、web.xml中加入struts2 mvc框架启动配置。
jar包:
struts2-core-XX.jar:struts2的核心包
xwork-2-XX.jar:xwork类库,struts2在其上构建
ognl-XX.jar:对象图导航语言(object graph navigation language),通过其读写对象属性
freemarker-XX.jar:struts2的ui标签模板使用freemarker
commons-logging:日志包
commons-fileupload--XX.jar:文件上传组件,必须加入
主要配置文件:struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<!-- START SNIPPET: xworkSample -->
<struts>
<!-- Some or all of these can be flipped to true for debugging -->
<constant name="struts.i18n.reload" value="false" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="false" />
<constant name="struts.configuration.xml.reload" value="false" />
<constant name="struts.custom.i18n.resources" value="globalMessages" />
<constant name="struts.action.extension" value="action,," />
<constant name="struts.convention.package.locators.basePackage" value="org.apache.struts2.showcase" />
<constant name="struts.convention.result.path" value="/WEB-INF" />
<!-- Necessary for Showcase because default includes org.apache.struts2.* -->
<constant name="struts.convention.exclude.packages" value="org.apache.struts.*,org.springframework.web.struts.*,org.springframework.web.struts2.*,org.hibernate.*"/>
<constant name="struts.freemarker.manager.classname" value="customFreemarkerManager" />
<constant name="struts.serve.static" value="true" />
<constant name="struts.serve.static.browserCache" value="false" />
<include file="struts-interactive.xml" />
<include file="struts-hangman.xml" />
<include file="struts-tags.xml"/>
<include file="struts-validation.xml" />
<include file="struts-actionchaining.xml" />
<include file="struts-fileupload.xml" />
<include file="struts-person.xml" />
<include file="struts-wait.xml" />
<include file="struts-token.xml" />
<include file="struts-model-driven.xml" />
<include file="struts-filedownload.xml" />
<include file="struts-conversion.xml" />
<include file="struts-freemarker.xml" />
<include file="struts-tiles.xml" />
<include file="struts-xslt.xml" />
<package name="default" extends="struts-default">
<interceptors>
<interceptor-stack name="crudStack">
<interceptor-ref name="checkbox" />
<interceptor-ref name="params" />
<interceptor-ref name="staticParams" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-action-ref name="showcase" />
<action name="showcase">
<result>/WEB-INF/showcase.jsp</result>
</action>
<action name="help">
<result>/WEB-INF/help.jsp</result>
</action>
<action name="viewSource" class="org.apache.struts2.showcase.source.ViewSourceAction">
<result>/WEB-INF/viewSource.jsp</result>
</action>
<action name="date" class="org.apache.struts2.showcase.DateAction" method="browse">
<result name="success">/WEB-INF/date.jsp</result>
</action>
</package>
<package name="skill" extends="default" namespace="/skill">
<default-interceptor-ref name="crudStack"/>
<action name="list" class="org.apache.struts2.showcase.action.SkillAction" method="list">
<result>/WEB-INF/empmanager/listSkills.jsp</result>
<interceptor-ref name="basicStack"/>
</action>
<action name="edit" class="org.apache.struts2.showcase.action.SkillAction">
<result>/WEB-INF/empmanager/editSkill.jsp</result>
<interceptor-ref name="params" />
<interceptor-ref name="basicStack"/>
</action>
<action name="save" class="org.apache.struts2.showcase.action.SkillAction" method="save">
<result name="input">/WEB-INF/empmanager/editSkill.jsp</result>
<result type="redirect">list.action</result>
</action>
<action name="delete" class="org.apache.struts2.showcase.action.SkillAction" method="delete">
<result name="error">/WEB-INF/empmanager/editSkill.jsp</result>
<result type="redirect">list.action</result>
</action>
</package>
<package name="employee" extends="default" namespace="/employee">
<default-interceptor-ref name="crudStack"/>
<action name="list" class="org.apache.struts2.showcase.action.EmployeeAction" method="list">
<result>/WEB-INF/empmanager/listEmployees.jsp</result>
<interceptor-ref name="basicStack"/>
</action>
<action name="edit-*" class="org.apache.struts2.showcase.action.EmployeeAction">
<param name="empId">{1}</param>
<result>/WEB-INF/empmanager/editEmployee.jsp</result>
<interceptor-ref name="crudStack"><param name="validation.excludeMethods">execute</param></interceptor-ref>
</action>
<action name="save" class="org.apache.struts2.showcase.action.EmployeeAction" method="save">
<result name="input">/WEB-INF/empmanager/editEmployee.jsp</result>
<result type="redirect">list.action</result>
</action>
<action name="delete" class="org.apache.struts2.showcase.action.EmployeeAction" method="delete">
<result name="error">/WEB-INF/empmanager/editEmployee.jsp</result>
<result type="redirect">list.action</result>
</action>
</package>
</struts>
web.xml中配置:struts1使用servlet启动,struts2使用filter来启动
<filter>
<filter-name>struts-prepare</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class>
</filter>
<filter>
<filter-name>struts-execute</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-prepare</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts-execute</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
StrutsPreparAndExecuteFilter的init方法会读取classpath下的struts.xml并完成初始化工作。注意:Struts2读取到struts.xml文件之后,以javabean的形式存放在内存中,以后struts2对用户的每次请求处理都将使用内存中的数据,而不是每次都读取struts.xml
第一个应用开发:
struts.xml:
struts2的包(package):<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <!-- START SNIPPET: xworkSample --> <struts> <package name="hn" namespace="/test" extends="struts-default"><action name="hw" class="action.HelloAction" method="execute"><result name="success">/WEB-INF/hello.jsp</result></action></package> </struts> <!-- END SNIPPET: xworkSample -->
struts2使用包来管理action,这个包和java的类包类似,用于管理一组业务功能相关的action,实际应用中,应该把一组业务功能相关的action放到一个包下面。
配置包必须指定name属性,如果其他包继承该包,必须通过该属性进行引用,namespace属性用于定义该包的命名空间,命名空间座位访问该包下Action路径的一部分,如访问上面那个action,访问路径为:/test/hw.action。namespace可不配置,默认命名空间慰空字符串。
通常每个包都继承struts-default包,因struts2很多核心功能通过拦截器实现,如:
从请求中把请求参数封装到action、文件上传和数据验证都通过拦截器实现。
struts-default定义了这些拦截器和Result类型,struts-default.xml也是struts默认配置文件,Struts2每次都会自动加载struts-default.xml文件。
包可以通过abstract="true"定义为抽象包,抽象包不能包含action。
访问过/test/hw之后,会交给HelloAction的execute方法去处理,处理完毕后,如果返回字符串为"success",则最终跳转到hello.jsp页面
Action名称搜索顺序