Struts 2.0 解读笔记

本文介绍了Struts2框架的核心架构,包括与Spring的整合、AJAX支持及多种模板集成等特性。通过具体示例展示了Struts2如何实现MVC模式,并深入探讨了其配置文件、验证机制以及DAO层的实现。

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

基于粒子群优化算法的p-Hub选址优化(Matlab代码实现)内容概要:本文介绍了基于粒子群优化算法(PSO)的p-Hub选址优化问题的研究与实现,重点利用Matlab进行算法编程和仿真。p-Hub选址是物流与交通网络中的关键问题,旨在通过确定最优的枢纽节点位置和非枢纽节点的分配方式,最小化网络总成本。文章详细阐述了粒子群算法的基本原理及其在解决组合优化问题中的适应性改进,结合p-Hub中转网络的特点构建数学模型,并通过Matlab代码实现算法流程,包括初始化、适应度计算、粒子更新与收敛判断等环节。同时可能涉及对算法参数设置、收敛性能及不同规模案例的仿真结果分析,以验证方法的有效性和鲁棒性。; 适合人群:具备一定Matlab编程基础和优化算法理论知识的高校研究生、科研人员及从事物流网络规划、交通系统设计等相关领域的工程技术人员。; 使用场景及目标:①解决物流、航空、通信等网络中的枢纽选址与路径优化问题;②学习并掌握粒子群算法在复杂组合优化问题中的建模与实现方法;③为相关科研项目或实际工程应用提供算法支持与代码参考。; 阅读建议:建议读者结合Matlab代码逐段理解算法实现逻辑,重点关注目标函数建模、粒子编码方式及约束处理策略,并尝试调整参数或拓展模型以加深对算法性能的理解。
内容概要:本文全面介绍了C#全栈开发的学习路径与资源体系,涵盖从基础语法到企业级实战的完整知识链条。内容包括C#官方交互式教程、开发环境搭建(Visual Studio、VS Code、Mono等),以及针对不同应用场景(如控制台、桌面、Web后端、跨平台、游戏、AI)的进阶学习指南。通过多个实战案例——如Windows Forms记事本、WPF学生管理系统、.NET MAUI跨平台动物图鉴、ASP.NET Core实时聊天系统及Unity 3D游戏项目——帮助开发者掌握核心技术栈与架构设计。同时列举了Stack Overflow、Power BI、王者荣耀后端等企业级应用案例,展示C#在高性能场景下的实际运用,并提供了高星开源项目(如SignalR、AutoMapper、Dapper)、生态工具链及一站式学习资源包,助力系统化学习与工程实践。; 适合人群:具备一定编程基础,工作1-3年的研发人员,尤其是希望转型全栈或深耕C#技术栈的开发者; 使用场景及目标:①系统掌握C#在不同领域的应用技术栈;②通过真实项目理解分层架构、MVVM、实时通信、异步处理等核心设计思想;③对接企业级开发标准,提升工程能力和实战水平; 阅读建议:此资源以开发简化版Spring学习其原理和内核,不仅是代码编写实现也更注重内容上的需求分析和方案设计,所以在学习的过程要结合这些内容一起来实践,并调试对应的代码。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值