读书人

struts 一与struts 2对比

发布时间: 2012-10-12 10:17:04 作者: rapoo

struts 1与struts 2对比

1.配置文件名称与目录位置

(1)struts1配置文件在/web-inf/*-config.xml

对应的web.xml配置方式为

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp_1260152591189">

<servlet>
??????? <servlet-name>action</servlet-name>
??????? <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
??????? <init-param>
??????????? <param-name>debug</param-name>
??????????? <param-value>2</param-value>
??????? </init-param>
??????? <init-param>
??????????? <param-name>config</param-name>
??????????? <param-value>/WEB-INF/struts-config.xml,/WEB-INF/yohey-config.xml</param-value>
??????? </init-param>
?<init-param>
??????????? <param-name>detail</param-name>
??????????? <param-value>2</param-value>
??????? </init-param>
??????? <init-param>
??????????? <param-name>locale</param-name>
??????????? <param-value>true</param-value>
??????? </init-param>
??????? <init-param>
??????????? <param-name>validate</param-name>
??????????? <param-value>true</param-value>
??????? </init-param>
??????? <load-on-startup>2</load-on-startup>
??? </servlet>
???
???
??? <servlet-mapping>
??????? <servlet-name>action</servlet-name>
??????? <url-pattern>*.do</url-pattern>
??? </servlet-mapping>

????<welcome-file-list>
??????? <welcome-file>index.html</welcome-file>
???????? <welcome-file>index.jsp</welcome-file>
??? </welcome-file-list>

</web-app>

(2)struts2配置文件在/src/struts.xml

对应的web.xml配置方式为

<web-app version="2.4"
?xmlns="http://java.sun.com/xml/ns/j2ee"
?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
?xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
?http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
?<filter>
??<filter-name>struts2</filter-name>
??<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
?</filter>
?<filter-mapping>
??<filter-name>struts2</filter-name>
??<url-pattern>/*</url-pattern>
?</filter-mapping>
</web-app>

?

2.action编写的规范不同

(1)struts1需要为页面form指定对应的ActionForm,并在*-config.xml中的 <form-beans>与<action-mapping>中匹配;

<action-mappings type="org.apache.struts.action.ActionMapping">

??<action type="com.resrefresh.ResRefreshAction" validate="true" scope="request" path="/ResRefreshAction">
???<forward name="ResRefreshBrowse" path="/soleplat/resrefresh/ResRefreshBrowse.jsp" />?
???<forward name="Accomplish" path="/soleplat/resrefresh/Accomplish.jsp" />
???<forward name="TypeView" path="/soleplat/resrefresh/TypeView.jsp" />?
???<forward name="ResView" path="/soleplat/resrefresh/ResView.jsp" />
???<forward name="RefreshAccomplish" path="/soleplat/resrefresh/RefreshAccomplish.jsp" />
???
??</action>

??<action type="com.txrun.page.TestPageAction" validate="true" scope="request" path="/TestPageAction">
???<forward name="TestPage" path="/TestPage.jsp" />
???
??</action>
??
??<action type="com.txrun.txrun.TxRunAction" validate="true" scope="request" path="/TxRunAction">
??? ?
??</action>
??
?</action-mappings>

?

(2)struts2摒弃了ActionForm与Action处理方法分开的特点,以及可以为action处理方法命多个名,这样在struts.xml文件中<action name="a" method="a">可以直接指定到“LoginAction类”的“a”方法为动作

<struts>
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.multipart.parser" value="jakarta" />
?<constant name="struts.multipart.maxSize" value="10485760" />
?<!-- debug -->
?<constant name="struts.devMode" value="true" />
?
?<!-- package -->
?<package name="struts2" extends="struts-default">
?
??<action name="login" method="a">
???<result name="success">/input.jsp</result>
???<result name="input">/login2.jsp</result>
??</action>
??
??<action name="pointConverter" class="com.test.action.PointAction">
???<result name="success">/output.jsp</result>
??</action>
?</package>
</struts>

3.页面的调用URL地址

(1)都可以为test.do?doing=login&name=aaa这样的形式;但现在知道的struts2中支持自定义名称的action servlet类处理方法,所以可以采用这样的调用方式test!login.do&name=aaa(提交到test名对应的class处理方法login)

(2)在Action类中

struts1必须继承org.apache.struts.action.Action,处理的方法只有一个public ActionForward perform(ActionMapping actionMapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){return actionMapping.findForward("errorPage");}

而struts2完全提供轻量级框架思想,可以不继承任何struts框架的基类,处理的方法可以自定义名称;

?

读书人网 >软件架构设计

热点推荐