[转]Struts 2.0 解读笔记

[转]Struts 2.0 解读笔记

 struts2 的demo 中的employee层可以清晰的暂时struts希望给开发者带来的架构

--------------------------

EmployeeAction <-- AbstractCRUDAction (web 层)
   ^
   |
EmployeeDao <-- AbstractDao <-- Dao (Domain 层,demo中也作为service存在于spring容器中)
   ^
   |
Employee <--IDEntity

--------------------------

struts.xml 配置

    <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>/empmanager/listSkills.jsp</result>
            <interceptor-ref name="basicStack"/>
        </action>
        <action name="edit" class="org.apache.struts2.showcase.action.SkillAction">
            <result>/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">/empmanager/editSkill.jsp</result>
            <result type="redirect">edit.action?skillName=${currentSkill.name}</result>
        </action>
        <action name="delete" class="org.apache.struts2.showcase.action.SkillAction" method="delete">
            <result name="error">/empmanager/editSkill.jsp</result>
            <result type="redirect">edit.action?skillName=${currentSkill.name}</result>  这里展现了struts2具有良好的url特性,比起ww的crud!edit让人舒服多了
        </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>/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>/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">/empmanager/editEmployee.jsp</result>
            <result type="redirect">edit-${currentEmployee.empId}.action</result>
        </action>
        <action name="delete" class="org.apache.struts2.showcase.action.EmployeeAction" method="delete">
            <result name="error">/empmanager/editEmployee.jsp</result>
            <result type="redirect">edit-${currentEmployee.empId}.action</result>
        </action>
    </package>
   
-----------------------------
每一个action有自己的properties文件,非常便于管理和代码生成,
当然也有一个全局的globalMessages.properties来存放一些save,delete这些都一样的字符
employee=Employee
employee.firstName=First Name
employee.lastName=Last Name
employee.description=Description

employee.id.required=Id is required
employee.lastName.required=Last Name is required
employee.birthDate.required=Birthdate is required
employee.backtolist=Back to Employee List

每个action自己的validation
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
<validators>
    <!-- Field-Validator Syntax -->
    <field name="currentEmployee.empId">
        <field-validator type="required">
            <message key="employee.id.required"/>
        </field-validator>
    </field>
    <field name="currentEmployee.lastName">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message key="employee.lastName.required"/>
        </field-validator>
    </field>
    <field name="currentEmployee.birthDate">
        <field-validator type="required">
            <message key="employee.birthDate.required"/>
        </field-validator>
    </field>
</validators>

--------------------------------
DAO是spring的bean,spring默认作为struts2的业务组件容器
在struts.properties中struts.objectFactory = spring

由于这里是autowire,Autowiring means to look for objects defined in Spring with the same name as your object property
所以action中所有用到IoC的地方,都会自动注入(因为action本质也是bean,下面会讲到)

<beans default-autowire="byName"> wire有四种方法:name, type, auto, constructor

    <bean id="skillDao" class="org.apache.struts2.showcase.dao.SkillDao"/>
    <bean id="employeeDao" class="org.apache.struts2.showcase.dao.EmployeeDao"/>
   
----------------------------------
struts2中的每一个action,也就是struts.xml里面声明的action,最终都将作为spring中的一个bean完成请求。
如果你不做额外的设定,struts2会自动生成这个bean。
如果你有特殊的需要,还可以在applicationContext.xml自己声明这个bean
你必须根据需要来决定是否再次声明
struts.xml
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <include file="struts-default.xml"/>

    <package name="default" extends="struts-default">
        <action name="foo" class="com.acme.Foo">  这个foo,在applicationConext.xml中没有声明,struts会自动生成一个bean
            <result>foo.ftl</result>
        </action>
    </package>

    <package name="secure" namespace="/secure" extends="default">
        <action name="bar" class="bar">           这个foo,在applicationConext.xml中声明了,struts会使用下面配置
            <result>bar.ftl</result>
        </action>
    </package>
</struts>

applicationConext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="autodetect">
    <bean id="bar" class="com.my.BarClass" singleton="false"/>
    ...
</beans>

-------------------------
无缝集成ajax
只要声明了theme="ajax",该标签控件的操作就被转换成ajax,请求发送到AjaxRemoteForm
Configured Editor configuration:
<s:form id="form2" action="AjaxRemoteForm" method="post">
    <s:textarea id="editor2" name="data" theme="ajax" cols="50" rows="10" value="Test Data 2">
        <s:param name="editorControls">textGroup;|;justifyGroup;|;listGroup;|;indentGroup</s:param>
    </s:textarea>
    <s:submit value="Submit"/>
</s:form>


------------------------
集成了多种模板
jsp,velocity,freemaker


------------------------

struts2 继承了ww2的优良传统,并且摈弃了ww2的一些缺陷,也可以看成是对ww2的一次重购。
无疑是目前最主流,最强大的MVC框架了。

它实现了和spring的完美结合

集成了ajax(dojo+dwr)

其插件支持jasperreports,jfreechart

你可能感兴趣的:([转]Struts 2.0 解读笔记)