struts2中用interceptor实现权限控制收藏
在jsp servlet中我们通常使用Servlet Filter控制用户是否登入, 是否有权限转到某个页面.在struts2中我们应该会想到他的拦截器(Interceptor), Interceptor在struts2中起着非常重要的作用. 很多struts2中的功能都是使用Interceptor实现的.
需求:简单的登入界面,让用户输入用户名,密码,记住密码(remember me). 如果用户选中remember me的话, 下次就不需要再登入了(使用cookie实现, 用需要点击logout取消remeber me功能). 如果用户起始输入的地址不是登入页面的话,在用户登入之后需要转到用户输入的起始地址.
我们先看看LoginInterceptor.java 引用:
package com.javaeye.dengyin2000.wallet.interceptor;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.StrutsStatics;
import com.javaeye.dengyin2000.wallet.dao.UserDAO;
import com.javaeye.dengyin2000.wallet.dao.UserNotFoundException;
import com.javaeye.dengyin2000.wallet.domains.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LoginInterceptor extends AbstractInterceptor {
public static final String USER_SESSION_KEY="wallet.session.user";
public static final String COOKIE_REMEMBERME_KEY="wallet.cookie.rememberme";
public static final String GOING_TO_URL_KEY="GOING_TO";
private UserDAO userDao;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
ActionContext actionContext = invocation.getInvocationContext();
HttpServletRequest request= (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);
Map session = actionContext.getSession();
if (session != null & session.get(USER_SESSION_KEY) != null){
return invocation.invoke();
}
Cookie[] cookies = request.getCookies();
if (cookies!=null) {
for (Cookie cookie : cookies) {
if (COOKIE_REMEMBERME_KEY.equals(cookie.getName())) {
String value = cookie.getValue();
if (StringUtils.isNotBlank(value)) {
String[] split = value.split("==");
String userName = split[0];
String password = split[1];
try {
User user = userDao
.attemptLogin(userName, password);
session.put(USER_SESSION_KEY, user);
} catch (UserNotFoundException e) {
setGoingToURL(session, invocation);
return "login";
}
} else {
setGoingToURL(session, invocation);
return "login";
}
return invocation.invoke();
}
}
}
setGoingToURL(session, invocation);
return "login";
}
private void setGoingToURL(Map session, ActionInvocation invocation){
String url = "";
String namespace = invocation.getProxy().getNamespace();
if (StringUtils.isNotBlank(namespace) & !namespace.equals("/")){
url = url + namespace;
}
String actionName = invocation.getProxy().getActionName();
if (StringUtils.isNotBlank(actionName)){
url = url + "/" + actionName + ".action";
}
session.put(GOING_TO_URL_KEY, url);
}
public UserDAO getUserDao() {
return userDao;
}
public void setUserDao(UserDAO userDao) {
this.userDao = userDao;
}
}首先判断session中有没有用户信息, 如果有的话继续, 如果没有的话,检查cookie中有没有rememberme的值,如果有的话,用==分割, 取得用户名密码进行登入.如果没有这个用户的话,记录下request的action地址然后转到登入页面.如果验证有这个用户,则继续下面的interceptor. 如果cookie中没有信息的话,则记录request的action地址然后转到登入页面. 以上就是LoginInterceptor的全部代码.
下面我们看看struts.xml 引用:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="loginInterceptor" class="loginInterceptor"></interceptor>
<interceptor-stack name="loginDefaultStack">
<interceptor-ref name="loginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="loginDefaultStack"></default-interceptor-ref>
<global-results>
<result name="login" type="redirect">/login.jsp</result>
</global-results>
<action name="index" class="indexAction">
<result>/index.jsp</result>
</action>
<action name="logout" class="logoutAction"></action>
<action name="login" class="loginAction" method="login">
<result type="redirect">${goingToURL}</result>
<result name="input">/login.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
<action name="register" class="registerAction">
<result type="redirect">/login.jsp</result>
<result name="input">/register.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>我们是使用的默认的interceptor stack是loginInterceptor, 如果你需要让不登入的用户也能访问的话,你需要配置你的action使用defaultStack. 我们这里的login, register使用的就是defaultStack. 这里要注意的是success的result是我们用LoginInterceptor设过来的值. 这样我们就能够转到用户输入的起始页面. 下面我们再来看看login.jsp 和 loginAction 引用:
<%@taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" c>
<title>Wallet-Login</title>
</head>
<body>
<h2>Login</h2>
<s:actionmessage/>
<s:actionerror/>
<s:form action="login" method="post" validate="false" theme="xhtml">
<s:textfield name="loginName" label="Username"></s:textfield><br/>
<s:password name="password" label="Password"></s:password><br/>
<s:checkbox label="Remember Me" name="rememberMe"></s:checkbox>
<s:submit value="%{'Login'}"></s:submit>
</s:form>
<a href="register.jsp">Register</a>
</body>
</html>
package com.javaeye.dengyin2000.wallet.actions;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.CookiesAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import com.javaeye.dengyin2000.wallet.dao.UserDAO;
import com.javaeye.dengyin2000.wallet.dao.UserNotFoundException;
import com.javaeye.dengyin2000.wallet.domains.User;
import com.javaeye.dengyin2000.wallet.interceptor.LoginInterceptor;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport implements ServletResponseAware, ServletRequestAware, SessionAware, CookiesAware{
private UserDAO userDao;
private String loginName;
private String password;
private boolean rememberMe;
private HttpServletResponse response;
private HttpServletRequest request;
private Map session;
private Map cookies;
private String goingToURL;
public String getGoingToURL() {
return goingToURL;
}
public void setGoingToURL(String goingToURL) {
this.goingToURL = goingToURL;
}
public boolean isRememberMe() {
return rememberMe;
}
public void setRememberMe(boolean rememberMe) {
this.rememberMe = rememberMe;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String login()throws Exception{
try {
User user = userDao.attemptLogin(loginName, password);
if (rememberMe){
Cookie cookie = new Cookie(LoginInterceptor.COOKIE_REMEMBERME_KEY, user.getLoginName() + "==" + user.getPassword());
cookie.setMaxAge(60 * 60 * 24 * 14);
response.addCookie(cookie);
}
session.put(LoginInterceptor.USER_SESSION_KEY, user);
String goingToURL = (String) session.get(LoginInterceptor.GOING_TO_URL_KEY);
if (StringUtils.isNotBlank(goingToURL)){
setGoingToURL(goingToURL);
session.remove(LoginInterceptor.GOING_TO_URL_KEY);
}else{
setGoingToURL("index.action");
}
return SUCCESS;
} catch (UserNotFoundException e) {
addActionMessage("user name or password is not corrected.");
return INPUT;
}
}
public UserDAO getUserDao() {
return userDao;
}
public void setUserDao(UserDAO userDao) {
this.userDao = userDao;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void setSession(Map session) {
this.session = session;
}
public void setCookiesMap(Map cookies) {
this.cookies = cookies;
}
}差不多就是这么多代码了. 最后看看logoutAction 引用:
package com.javaeye.dengyin2000.wallet.actions;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.javaeye.dengyin2000.wallet.interceptor.LoginInterceptor;
import com.opensymphony.xwork2.ActionSupport;
public class LogoutAction extends ActionSupport implements ServletRequestAware , ServletResponseAware{
private HttpServletRequest request;
private HttpServletResponse response;
public String execute() throws Exception{
HttpSession session = request.getSession(false);
if (session!=null)
session.removeAttribute(LoginInterceptor.USER_SESSION_KEY);
Cookie[] cookies = request.getCookies();
if (cookies!=null) {
for (Cookie cookie : cookies) {
if (LoginInterceptor.COOKIE_REMEMBERME_KEY.equals(cookie
.getName())) {
cookie.setValue("");
cookie.setMaxAge(0);
response.addCookie(cookie);
return "login";
}
}
}
return "login";
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
}这里需要注意的是需要把cookie也清理下.
applicationContext-struts.xml 引用:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- Example of SAF2 action instantiated by Spring -->
<!-- bean id="helloWorldAction" class="tutorial.HelloWorldAction" singleton="false" />
-->
<bean id="indexAction" class="com.javaeye.dengyin2000.wallet.actions.IndexAction" singleton="false"></bean>
<bean id="loginAction" class="com.javaeye.dengyin2000.wallet.actions.LoginAction" singleton="false">
<property name="userDao" ref="userDao" />
</bean>
<bean id="logoutAction" class="com.javaeye.dengyin2000.wallet.actions.LogoutAction" singleton="false"></bean>
<bean id="registerAction" class="com.javaeye.dengyin2000.wallet.actions.RegisterAction" singleton="false"></bean>
<!-- the following is struts2 interceptors -->
<bean id="loginInterceptor" class="com.javaeye.dengyin2000.wallet.interceptor.LoginInterceptor">
<property name="userDao" ref="userDao" />
</bean>
<bean id="userDao" class="com.javaeye.dengyin2000.wallet.dao.UserDAOImpl">
</bean>
</beans>
需求:简单的登入界面,让用户输入用户名,密码,记住密码(remember me). 如果用户选中remember me的话, 下次就不需要再登入了(使用cookie实现, 用需要点击logout取消remeber me功能). 如果用户起始输入的地址不是登入页面的话,在用户登入之后需要转到用户输入的起始地址.
我们先看看LoginInterceptor.java 引用:
package com.javaeye.dengyin2000.wallet.interceptor;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.StrutsStatics;
import com.javaeye.dengyin2000.wallet.dao.UserDAO;
import com.javaeye.dengyin2000.wallet.dao.UserNotFoundException;
import com.javaeye.dengyin2000.wallet.domains.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LoginInterceptor extends AbstractInterceptor {
public static final String USER_SESSION_KEY="wallet.session.user";
public static final String COOKIE_REMEMBERME_KEY="wallet.cookie.rememberme";
public static final String GOING_TO_URL_KEY="GOING_TO";
private UserDAO userDao;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
ActionContext actionContext = invocation.getInvocationContext();
HttpServletRequest request= (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);
Map session = actionContext.getSession();
if (session != null & session.get(USER_SESSION_KEY) != null){
return invocation.invoke();
}
Cookie[] cookies = request.getCookies();
if (cookies!=null) {
for (Cookie cookie : cookies) {
if (COOKIE_REMEMBERME_KEY.equals(cookie.getName())) {
String value = cookie.getValue();
if (StringUtils.isNotBlank(value)) {
String[] split = value.split("==");
String userName = split[0];
String password = split[1];
try {
User user = userDao
.attemptLogin(userName, password);
session.put(USER_SESSION_KEY, user);
} catch (UserNotFoundException e) {
setGoingToURL(session, invocation);
return "login";
}
} else {
setGoingToURL(session, invocation);
return "login";
}
return invocation.invoke();
}
}
}
setGoingToURL(session, invocation);
return "login";
}
private void setGoingToURL(Map session, ActionInvocation invocation){
String url = "";
String namespace = invocation.getProxy().getNamespace();
if (StringUtils.isNotBlank(namespace) & !namespace.equals("/")){
url = url + namespace;
}
String actionName = invocation.getProxy().getActionName();
if (StringUtils.isNotBlank(actionName)){
url = url + "/" + actionName + ".action";
}
session.put(GOING_TO_URL_KEY, url);
}
public UserDAO getUserDao() {
return userDao;
}
public void setUserDao(UserDAO userDao) {
this.userDao = userDao;
}
}首先判断session中有没有用户信息, 如果有的话继续, 如果没有的话,检查cookie中有没有rememberme的值,如果有的话,用==分割, 取得用户名密码进行登入.如果没有这个用户的话,记录下request的action地址然后转到登入页面.如果验证有这个用户,则继续下面的interceptor. 如果cookie中没有信息的话,则记录request的action地址然后转到登入页面. 以上就是LoginInterceptor的全部代码.
下面我们看看struts.xml 引用:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="loginInterceptor" class="loginInterceptor"></interceptor>
<interceptor-stack name="loginDefaultStack">
<interceptor-ref name="loginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="loginDefaultStack"></default-interceptor-ref>
<global-results>
<result name="login" type="redirect">/login.jsp</result>
</global-results>
<action name="index" class="indexAction">
<result>/index.jsp</result>
</action>
<action name="logout" class="logoutAction"></action>
<action name="login" class="loginAction" method="login">
<result type="redirect">${goingToURL}</result>
<result name="input">/login.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
<action name="register" class="registerAction">
<result type="redirect">/login.jsp</result>
<result name="input">/register.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>我们是使用的默认的interceptor stack是loginInterceptor, 如果你需要让不登入的用户也能访问的话,你需要配置你的action使用defaultStack. 我们这里的login, register使用的就是defaultStack. 这里要注意的是success的result是我们用LoginInterceptor设过来的值. 这样我们就能够转到用户输入的起始页面. 下面我们再来看看login.jsp 和 loginAction 引用:
<%@taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" c>
<title>Wallet-Login</title>
</head>
<body>
<h2>Login</h2>
<s:actionmessage/>
<s:actionerror/>
<s:form action="login" method="post" validate="false" theme="xhtml">
<s:textfield name="loginName" label="Username"></s:textfield><br/>
<s:password name="password" label="Password"></s:password><br/>
<s:checkbox label="Remember Me" name="rememberMe"></s:checkbox>
<s:submit value="%{'Login'}"></s:submit>
</s:form>
<a href="register.jsp">Register</a>
</body>
</html>
package com.javaeye.dengyin2000.wallet.actions;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.CookiesAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import com.javaeye.dengyin2000.wallet.dao.UserDAO;
import com.javaeye.dengyin2000.wallet.dao.UserNotFoundException;
import com.javaeye.dengyin2000.wallet.domains.User;
import com.javaeye.dengyin2000.wallet.interceptor.LoginInterceptor;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport implements ServletResponseAware, ServletRequestAware, SessionAware, CookiesAware{
private UserDAO userDao;
private String loginName;
private String password;
private boolean rememberMe;
private HttpServletResponse response;
private HttpServletRequest request;
private Map session;
private Map cookies;
private String goingToURL;
public String getGoingToURL() {
return goingToURL;
}
public void setGoingToURL(String goingToURL) {
this.goingToURL = goingToURL;
}
public boolean isRememberMe() {
return rememberMe;
}
public void setRememberMe(boolean rememberMe) {
this.rememberMe = rememberMe;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String login()throws Exception{
try {
User user = userDao.attemptLogin(loginName, password);
if (rememberMe){
Cookie cookie = new Cookie(LoginInterceptor.COOKIE_REMEMBERME_KEY, user.getLoginName() + "==" + user.getPassword());
cookie.setMaxAge(60 * 60 * 24 * 14);
response.addCookie(cookie);
}
session.put(LoginInterceptor.USER_SESSION_KEY, user);
String goingToURL = (String) session.get(LoginInterceptor.GOING_TO_URL_KEY);
if (StringUtils.isNotBlank(goingToURL)){
setGoingToURL(goingToURL);
session.remove(LoginInterceptor.GOING_TO_URL_KEY);
}else{
setGoingToURL("index.action");
}
return SUCCESS;
} catch (UserNotFoundException e) {
addActionMessage("user name or password is not corrected.");
return INPUT;
}
}
public UserDAO getUserDao() {
return userDao;
}
public void setUserDao(UserDAO userDao) {
this.userDao = userDao;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void setSession(Map session) {
this.session = session;
}
public void setCookiesMap(Map cookies) {
this.cookies = cookies;
}
}差不多就是这么多代码了. 最后看看logoutAction 引用:
package com.javaeye.dengyin2000.wallet.actions;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.javaeye.dengyin2000.wallet.interceptor.LoginInterceptor;
import com.opensymphony.xwork2.ActionSupport;
public class LogoutAction extends ActionSupport implements ServletRequestAware , ServletResponseAware{
private HttpServletRequest request;
private HttpServletResponse response;
public String execute() throws Exception{
HttpSession session = request.getSession(false);
if (session!=null)
session.removeAttribute(LoginInterceptor.USER_SESSION_KEY);
Cookie[] cookies = request.getCookies();
if (cookies!=null) {
for (Cookie cookie : cookies) {
if (LoginInterceptor.COOKIE_REMEMBERME_KEY.equals(cookie
.getName())) {
cookie.setValue("");
cookie.setMaxAge(0);
response.addCookie(cookie);
return "login";
}
}
}
return "login";
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
}这里需要注意的是需要把cookie也清理下.
applicationContext-struts.xml 引用:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- Example of SAF2 action instantiated by Spring -->
<!-- bean id="helloWorldAction" class="tutorial.HelloWorldAction" singleton="false" />
-->
<bean id="indexAction" class="com.javaeye.dengyin2000.wallet.actions.IndexAction" singleton="false"></bean>
<bean id="loginAction" class="com.javaeye.dengyin2000.wallet.actions.LoginAction" singleton="false">
<property name="userDao" ref="userDao" />
</bean>
<bean id="logoutAction" class="com.javaeye.dengyin2000.wallet.actions.LogoutAction" singleton="false"></bean>
<bean id="registerAction" class="com.javaeye.dengyin2000.wallet.actions.RegisterAction" singleton="false"></bean>
<!-- the following is struts2 interceptors -->
<bean id="loginInterceptor" class="com.javaeye.dengyin2000.wallet.interceptor.LoginInterceptor">
<property name="userDao" ref="userDao" />
</bean>
<bean id="userDao" class="com.javaeye.dengyin2000.wallet.dao.UserDAOImpl">
</bean>
</beans>
相关推荐
Interceptor是Struts2的一大特色,它们按照预定义的顺序在Action执行前后被调用,提供了如日志、权限验证、事务管理等功能。开发者可以创建自定义拦截器,实现特定的业务逻辑,使得代码结构更加清晰,可复用性增强。...
此外,`Interceptor`接口是拦截器的基石,它们在动作执行前后进行拦截,实现如权限检查、日志记录等功能。 Struts2中文帮助文档则针对这些API提供了更直观易懂的中文解释,涵盖了从基础知识到高级特性的各种主题。...
4. **Interceptor(拦截器)**:拦截器是Struts2中的一个重要特性,允许在Action调用前后插入自定义逻辑,如日志、权限检查、事务管理等。开发者可以通过实现`com.opensymphony.xwork2.interceptor.Interceptor`接口...
Struts2通过配置文件(如struts.xml)定义Action、Interceptor和Result,实现URL映射和流程控制。同时,系统支持多语言,可以为不同的地区提供本地化的界面。 6. **性能优化** 为了提高性能,Struts2支持缓存...
3. **Interceptor**:拦截器是Struts2的一个重要特性,它们是实现了特定接口的类,能够在Action执行前后进行拦截,实现如日志记录、权限验证、数据校验等通用功能。 4. **Value Stack**:值栈是Struts2中用于存储和...
3. **Interceptor拦截器**:拦截器是Struts2的特色之一,它们在Action执行前后执行特定的任务,比如日志记录、权限验证等。开发者可以通过实现Interceptor接口或继承自DefaultActionInterceptor来创建自定义拦截器。...
- ValueStack是Struts2中用于数据传递的对象栈,它允许在Action和视图之间共享数据。 - OGNL(Object-Graph Navigation Language)是一种强大的表达式语言,用于在ValueStack中查找、设置和调用对象的属性,简化了...
Struts2通过Action类实现控制器功能,通过Result和Interceptor实现模型和视图的交互。 **2. Action类** Action类是Struts2的核心组件,它是处理用户请求的入口点。开发者需要创建自定义的Action类,该类通常会实现...
3. **Interceptor(拦截器)**:拦截器是Struts2的一大特色,它定义了一组在Action执行前后执行的方法,用于实现如日志记录、权限检查、数据校验等通用功能。 4. **ValueStack**:ValueStack是Struts2中用来存储...
Struts2的拦截器机制使得开发者可以定义拦截器来处理通用的任务,如日志记录、事务管理、权限控制等,提高了代码的复用性和可维护性。 **6. FilterDispatcher与.struts2-convention-plugin** FilterDispatcher是...
6. **拦截器实战**:编写自定义拦截器,例如实现权限控制、日志记录等。 7. **分页与排序**:演示如何在Struts2中实现数据的分页显示和排序功能。 8. **Maven项目构建**:使用Maven构建Struts2项目,管理依赖库,...
2. **拦截器(Interceptor)**:Struts2的拦截器是其核心特性之一,它们按照预设的顺序在Action调用前后执行,提供如日志、权限检查、事务管理等功能。你可以创建自定义拦截器,并在`struts.xml`中配置其执行顺序。 ...
在Web开发领域,Struts2以其MVC(Model-View-Controller)设计模式为基础,提供了一种组织和控制应用程序逻辑的方式。这次我们关注的是Struts2的2.2.1.1版本的源代码。 该版本的源码是为了解决特定的安全问题,特别...
拦截器是Struts2的核心特性,它们可以定义在Action前或后执行,实现如日志记录、权限验证等功能。此外,本章还将介绍ValueStack,它是Struts2中用于存储和传递数据的重要组件,允许在Action和视图之间共享数据。 ...
Struts2的体系结构主要由四大部分组成:控制器(Controller)、模型(Model)、视图(View)以及拦截器(Interceptor)。其中,控制器负责接收用户请求并调度到相应的Action;模型包含了业务逻辑和数据;视图用于...
Struts2的核心组件包括Action、Interceptor、Result和ValueStack等,这些组件共同协作,实现了业务逻辑与表现层的分离,提高了代码的可维护性和可测试性。 二、Action Action是Struts2中的核心元素,代表一次用户...
- **Interceptor**:拦截器是Struts2的一大特色,它允许开发者在Action调用前后插入自定义的处理逻辑,实现如日志记录、权限检查等功能。 - **Value Stack**:值栈是Struts2中用于存放Action实例和Ognl表达式的...
3. **Interceptor**:Interceptor是Struts2的重要特性,用于在Action执行前后插入自定义逻辑,如日志、权限验证等。文档会解释Interceptor的生命周期和如何编写自定义Interceptor。 4. **Action与Result**:Action...