Struts2 流程 ,解耦,耦合方式获取session,application,request

Struts2是有哪两个框架组成:

struts1和WebWork
2.struts目录
apps
doc
src
lib

3.如何让struts2成为action
实现接口重写excute

public String excute(){

return "SUCCESS"

}

4.5个 大写静态常量
SUCCESS LOGIN ERROR INPUT NONE
5.filter过滤器


actionContext(动作上下文)

6
<filter>
<filter-name>struts</filter-name>
<filter-class>cn.struts.2</filter-class>
</filter>

<fiter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


配置struts 文件说明 配置文件中添加如下配置就你不用重启服务
<constant name="struts.devmode" value=true">


public class LoginAction implements Action{
private String username = "";
private String password = "";
public String execute() throws Exception {
if (username.equals("1")&&password.equals("1")) {
return SUCCESS;
}else {
return LOGIN;
}

}
}
看下面登陆案例 Action类可以直接封装属性和字段的并且记得封装get
set
因为底层是set注入


关于自动装配问题


一个类中植入实例类 在判断一下并返回搁到输入的值
***************************************
Struts 2访问Servlet API
解耦方式来个两种获取session
//方案1将用户名放入session域两种方案 解耦,这是通过getContext 方式获取session
//ActionContext.getContext().getSession()

//方案二:向Action中注入ServletAPI对象对应的Map对象


什么是解耦方式?
Map<String,object> request=(Map<String,Object>)ActionContext.getContext().get("request");

Map<String,object>session=ActionContext.getContext().getSession();

Map<String,object>application=ActionContext.getContext().getApplication();

////////////////////////***********************************/////////////////////
什么是耦合方式?

ActionContext.getContext().getApplication();
ActionContext.getContext().getSession();
ActionContext.getContext().getRequest();

***********************

Struts2 优势:

有两方面的技术优势设计的第一目标就是使MVC模式应用于web程序设计
Struts2原理


Struts2 流程
web.xml
action
result
struts.xml

***********

页面 → 核心控制器 →Action →Result →页面
ServletApi 接口

HttpServletRequest
?HttpservletResponse
ServletRequest
ServletResponse
ServletContext
ServletConfig

原文地址:https://www.cnblogs.com/hualishu/p/9964645.html