Struts2 Notes: To introduce what Struts2 has and how to use Struts2 . Don't pay attention to the principle and core for the time being .
Learning the use of Struts2 should be from the following three aspects
①How to jump, <result> tag, <package> tag, DMI
②VS(ValueStack) and ognl expression, Struts2 label
③Interceptor
Summary of this article: Pre-content (MVC development mode, construction of Struts2 development environment), jump, <result> tag <package> tag usage, DMI
One: MVC development mode
I. Concept: MVC is a design idea that divides process control, business operations, and data display into three levels (modules), which exist independently and perform their own duties. They are a combination and invocation of each other.
II. The composition of MVC:
M |
Model |
Model |
A representation or simulation of the objective world (business simulation, object simulation) |
Service+DAO+Entity |
V |
View |
view |
Responsible for page display and data collection |
HTML(JSP)+JS |
C |
Controller |
controller |
Responsible for process control and program direction |
Servlet(Action) |
III. Features of MVC:
1). Low coupling: The correlation between modules is not strong, and there is no inseparable relationship with a specific implementation.
2). High maintenance: based on low coupling, it is easier to modify or replace different modules, hot swap.
3). High reusability: The same data operations can serve different business functions, improving reusability.
Second, the Struts2 framework:
1. Concept: It acts as a Controller in MVC and can replace the original Servlet (Action).
2. Problems with using Servlet as controller:
3. Use Struts as a controller:
Three: Struts2 development environment to build
https://blog.csdn.net/sugar_map/article/details/80171920
四:Struts2编码 (暂不通过值栈取值、通过原生作用域取值)
1:实现Action接口 import com.opensymphony.xwork2.Action;
实现execute()方法
①:通过ServletActionContext对象获取作用域对象。
获取到request可以收集客户端传来的值、将值绑定到request作用域中,通过转发传值。
②:方法的返回值类型为 String
返回null值:响应空白页面。
<result>标签的name属性与返回值匹配:响应对应的Action或JSP页面。
若方法的返回值 与<result>标签的name属性值不匹配,则报错。
细节: Action对象的创建:
a. 多次请求同一个Servlet只创建一个Servlet对象。
b. 多次请求同一个Action对象会创建多个Action对象(每次请求都会创建一个)。
五、<package>包标签:
1. 作用:
I. 按功能完成模块的划分,管理不同<action>标签。
II. 功能的复用。
六:Struts2跳转详情
分为两大类:
Action->JSP,Action->Action
①首先讨论 同包package的情况
1. Action ~ JSP:
I. dispatcher(默认):URL不变
<!-- Action->JSP 默认方式为dispatcher(可以省略)-->
<action name="showAll" class="action.EmployeeListAction">
<result name="success" type="dispatcher">emplist.jsp</result>
</action>
II. redirect:URL改变
<!--Action->JSP type="redirect"(不可省略)-->
<action name="login" class="action.ControlLoginAction">
<result name="success" type="chain">showAll</result>
<result name="none" type="redirect">login.jsp</result>
</action>
2. Action ~ Action:
I. chain:URL不变
<!--Action->JSP type="chain"(不可省略)-->
<action name="login" class="action.ControlLoginAction">
<result name="success" type="chain">showAll</result>
<result name="none" type="redirect">login.jsp</result>
</action>
<!--Action->JSP type="redirectAction"(不可省略)-->
II. redirectAction:URL改变
<action name="showAll" class="action.EmployeeListAction">
<result name="success" type="dispatcher">emplist.jsp</result>
</action>
<action name="delete" class="action.ControlDeleteAction">
<result name="success" type="redirectAction">showAll</result>
</action>
②:不同包的情况
Action->JSP跟同包情况一致。
Action到Action的重定向(不讨论转发,因为Action为多例,不方便共享作用域。)
七:DMI(动态方法的调用)
1. 将对同一张表操作的多个请求操作,书写在一个Action中,从而减少系统Action的数量,便于后续的项目管理与维护。
3. 逻辑代码:
建议:继承ActionSupport,由父类提供默认实现。 //因实现Action接口必须覆盖execute()方法(稍显多余)
4. struts.xml配置文件:
根据method属性区分Action请求的到底是哪个方法。