struts2使用ServletAPI笔记

我的梦境 提交于 2020-03-17 10:58:57

ServletAPI

在struts2的开发中,依然需要使用到servlet的api,比如:用户的登录后需要将用户信息保存到session中。在struts2中有两种方式可以获取servlet的api,一种是解耦方式,一种是耦合方式。

解耦方式
  • web.xml
<!-- struts2的前端控制器 -->
<filter>
	<filter-name>struts2</filter-name>
	<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>struts2</filter-name>
	<url-pattern>*.action</url-pattern>
</filter-mapping>
  • 配置文件
<struts>
	<package name="default" namespace="/" extends="struts-default">
		<action name="login" class="com.x.action.LoginAction" method="login">
			<result name="success">/success.jsp</result>
			<result name="login">/login.jsp</result>
		</action>
	</package>
</struts>
  • 实体类
public class User {
	//保证有无参构造方法
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}
  • 处理类
public class LoginAction implements ModelDriven<User>{
	private User user = new User();
	public User getModel() {
		return user;
	}
	
	public User getUser() {
		return user;
	}
	
	public void setUser(User user) {
		this.user = user;
	}
	//处理登录业务
	public String login() {
		if("zs".equals(user.getUsername()) && "111".equals(user.getPassword())) {
			//用户登录成功,将用户信息写入session
			ActionContext.getContext().getSession().put("currentuser", user);
			//获取application
			ActionContext.getContext().getApplication();
			//获取request
			Map<String, Object> req = (Map<String, Object>) ActionContext.getContext().get("request");
			req.put("pwd", user.getPassword());
			return "success";
		}else {
			return Action.LOGIN;
		}
	}
}
  • 页面
<body>
	<form action="login.action" method="post">
		username:<input type="text" name="username"><br>
		password:<input type="password" name="password"><br>
		<input type="submit">
	</form>
</body>
<body>
登录成功,欢迎${sessionScope.currentuser.username }
</body>

耦合方式,通过servletapi
  • ①直接通过ServletActionContext来获取
//处理登录业务
public String login() {
		if("zs".equals(user.getUsername()) && "111".equals(user.getPassword())) {
			//用户登录成功
			HttpServletRequest req = ServletActionContext.getRequest();
			req.getSession().setAttribute("currentuser", user);
			return "success";
		}else {
			return Action.LOGIN;
		}
}
  • ②通过ActionContext对象获取HttpServletContext对象
//处理登录业务
public String login() {
	if("zs".equals(user.getUsername()) && "111".equals(user.getPassword())) {
		//用户登录成功
		HttpServletRequest req = (HttpServletRequest) ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
		req.getSession().setAttribute("currentuser", user);
		return "success";
	}else {
		return Action.LOGIN;
	}
}
  • ③通过IOC方式直接获取servletapi对象

实现接口ServletRequestAware获取request对象

public class LoginAction implements ModelDriven<User>,ServletRequestAware{
	private User user = new User();
	private HttpServletRequest req;
	//处理登录业务
	public String login() {
		if("zs".equals(user.getUsername()) && "111".equals(user.getPassword())) {
			//用户登录成功
			req.getSession().setAttribute("currentuser", user);
			return "success";
		}else {
			return Action.LOGIN;
		}
	}
	@Override
	//有IOC注入request对象
	public void setServletRequest(HttpServletRequest request) {
		this.req = request;
	}
	public User getModel() {
		return user;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
}
  • 以上三种方式在本质上是一种,都是第二种。
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!