我正在使用Struts 2创建一个Web应用程序。我有一个登录页面。当用户在输入用户名和密码后单击登录按钮时,将检查凭据,如果发现凭据正确,则会创建会话并设置其属性,并将控制重定向到WELCOME JSP。
现在在打开welcome.jsp
之前,我想检查是否设置了会话属性。如何在Struts 2中做到这一点?
任何人都可以告诉我拦截器的概念。我读到我们创建拦截器以在调用动作之前或之后执行任何功能。我是否可以在每次调用WELCOME JSP之前创建一个检查会话是否已设置的拦截器。
答案 0 :(得分:5)
您可以使用<s:if>
标记来测试Session中您的属性的存在。我假设您在动作类中设置了值,这是您在jsp页面中的设置方法
<s:if test="%{#session.logged_in ==true}">
我假设在这里我设置一个标志,指示用户是否以类似的方式登录,您可以根据您的要求进行测试
对于拦截器,它们是由Struts2开箱即用的实用程序类,以使您的生活更轻松.Interceptors只是具有某些功能的java类,由框架提供,其中一些是
根据应用程序中配置的拦截器堆栈调用这些拦截器,它们将在2个位置调用
在第一种情况下,它们将在您的动作执行或您定义的自定义方法被调用之前被调用,拦截器负责向您的动作类提供所需的数据,拦截器在您的动作被调用之前完成的一些工作
struts2提供了一组拦截器,你可以查看struts-defaultxml。
您可以创建任意数量的拦截器并将其配置为根据您的要求执行,您需要扩展AbstractInterceptor
并在intercept(ActionInvocation invocation)
方法中提供自定义逻辑
我建议您按照下面提到的链接获取更多概述
building-your-own-interceptor Struts2 Interceptors writing-interceptors
答案 1 :(得分:3)
@rickgaurav第一个问题。像这样做一个登录动作
<action name="login_action" class="loginAction class">
<result name="success" type="chain">welcomeAction</result>
<result name="input">/index.jsp</result>
<result name="error">/index.jsp</result>
</action>
你的index.jsp是你的登录页面
并在登录拦截器中首先创建会话映射,其中您的会话属性将存储
Map<String, Object> sessionAttributes = invocation
.getInvocationContext().getSession();
使用此条件进行检查后
if (sessionAttributes == null
|| sessionAttributes.get("userName") == null
|| sessionAttributes.get("userName").equals("")) {
return "login";
}else{
if (!((String) sessionAttributes.get("userName")).equals(null)){
return invocation.invoke();
}else{
return "login";
}
对于您的第二个问题,假设您正在调用welcomeAction来访问welcome.jsp页面 所以你可以像这样添加动作
<action name="welcomeAction" class="class">
<interceptor-ref name="logininterceptor"></interceptor-ref>
<result name="login" type="chain">loginaction</result>
<result name="success" >/welcome.jsp</result>
</action>
希望这对你有用
答案 2 :(得分:2)
在Struts.xml中写下这个:
<interceptors>
<interceptor name="loginInterceptor"
class="com.mtdc.interceptor.LoginInterceptor"></interceptor>
<interceptor-stack name="loginStack">
<interceptor-ref name="loginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
2.现在你想要检查会话的动作之后,请输入:<interceptor-ref name="loginStack"></interceptor-ref>
3.我们在struts.xml中的LoginACtion:`
<action name="login_action" class="com.action.LoginAction">
<result name="success">/welcome.jsp</result>
<result name="input">/login.jsp</result>
<result name="error">/login.jsp</result>
</action>
`
4.现在为make make,interceptor.LoginInterceptor:
定义LOgin拦截器`
public String intercept(ActionInvocation invocation) throws Exception {
Map<String, Object> sessionAttributes = invocation
.getInvocationContext().getSession();
if (sessionAttributes == null
|| sessionAttributes.get("userName") == null
|| sessionAttributes.get("userName").equals("")) {
return "login";
} else {
if (!((String) sessionAttributes.get("userName")).equals(null)
|| !((String) sessionAttributes.get("userName")).equals("")) {
return invocation.invoke();
} else {
return "login";
}
}
}
`
[5]。现在,在您指定会话的位置创建Login Action类:
`
public String execute() {
String result = LOGIN;
UserLogin userlogin;
try {
userlogin = userlogindao.authenticateUser(signin_username, signin_password);
if (userlogin != null && userlogin.getUsername() != null) {
session.put("userID", userlogin.getUserId());
session.put("userName", userlogin.getUsername());
result = SUCCESS;
} else {
result = LOGIN;
}
} catch (Exception e) {
result = LOGIN;
e.printStackTrace();
}
return result;
}
`
[6]。现在点击呼叫操作LoginAction登录页面的最后一步。
答案 3 :(得分:0)
是的,你可以使用拦截器。 创建一个Action类,它将在会话中存储用户的UserId / Username(在用户通过身份验证之后)。
session.put("userID", userlogin.getUserId());
之后创建一个impl的Interceptor类。拦截器
public class LoginInterceptor implements **Interceptor** {
public String intercept(ActionInvocation invocation) throws Exception {
使用
获取会话属性sessionAttributes.get("userId");
如果为null则检查它是否为null然后返回登录以将用户转发到登录页面 或者返回invocation.invoke();继续行动。
并在struts.xml文件中配置包内的拦截器
<interceptors>
<interceptor name="loginInterceptor" class=".....LoginInterceptor"></interceptor>
<interceptor-stack name="loginStack">
<interceptor-ref name="loginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
你可以写下行动
<action name="anything" class="anyclass" method="default">
<interceptor-ref name="loginStack"></interceptor-ref>
<result name="login">/index.jsp</result>
<result name="success" type="redirect">success.jsp</result>
</action>