struts2应用程序中的会话

Mih*_*ala 10 java session struts2 java-ee

我创建了一个Web应用程序, 如果有用户会话,我需要维护会话,然后只有这样才能让用户看到jsp.

我之前使用过jsp servlet,但我是struts2的新手.

这里我在我的动作类中设置用户名:

修订后的守则

private HttpSession session;

public void setSession(HttpSession session) {
    // TODO Auto-generated method stub0
    this.session = session;
}

public HttpSession getSession() {
    return session;
}

public String getLoginStatus(){     
    session = request.getSession();
    session.setAttribute("userName", loginBean.getUsername());
    return SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

现在,当我在操作后重定向到下一页时,它会显示一次会话值.之后,在每个页面上,我在会话中找到空值.

<%
    String userName = (String)session.getAttribute("userName");             
    System.out.println(userName);                        

    if(userName == null || userName.equals("") ){
        response.sendRedirect("login.jsp");
    }

%>
Run Code Online (Sandbox Code Playgroud)

我在某处读到动作类会话的范围仅限于一页 - 我该如何解决这个问题?

任何例子都对我很有帮助.

Ste*_*tez 35

您目前拥有的代码存在一些问题.

  • 您应该使用Interceptor强制用户登录,而不是尝试在JSP中强制执行它.JSP应仅用于表示,而不用于流控制.
  • 您应该避免在JSP中使用scriptlet(代码块).很久以前就被弃用了,并且在MVC应用程序中被广泛认为是一种非常糟糕的做法.
  • 您可以直接访问JSP中的会话值.SessionAware除非您需要访问操作本身内部的会话,否则无需在操作中实现该接口.
  • 您应该将用户重定向到登录操作,而不是直接重定向到JSP页面,否则您将绕过Struts2框架并失去使用框架的好处.

登录示例

下面是使用Struts2框架创建基本登录系统的一些示例代码.

要求登录

这部分是可选的,但通常,并非Web应用程序中的所有页面都需要用户登录.因此,让我们创建一个名为的接口LoginRequired.如果用户尚未登录,则实现此标记接口的任何操作都将重定向到登录页面.

注意:如果您愿意,可以使用注释,但是对于此示例,我将使用该界面.

public interface LoginRequired {}
Run Code Online (Sandbox Code Playgroud)

拦截器

拦截器将处理强制用户登录任何实现LoginRequired接口的请求操作.

public class LoginInterceptor extends AbstractInterceptor {
    @Override
    public String intercept(final ActionInvocation invocation) throws Exception {
        Map<String, Object> session = ActionContext.getContext().getSession();

        // sb: feel free to change this to some other type of an object which
        // represents that the user is logged in. for this example, I am using
        // an integer which would probably represent a primary key that I would
        // look the user up by with Hibernate or some other mechanism.
        Integer userId = (Integer) session.get("userId");

        // sb: if the user is already signed-in, then let the request through.
        if (userId != null) {
            return invocation.invoke();
        }

        Object action = invocation.getAction();

        // sb: if the action doesn't require sign-in, then let it through.
        if (!(action instanceof LoginRequired)) {
            return invocation.invoke();
        }

        // sb: if this request does require login and the current action is
        // not the login action, then redirect the user
        if (!(action instanceof LoginAction)) {
            return "loginRedirect";
        }

        // sb: they either requested the login page or are submitting their
        // login now, let it through
        return invocation.invoke();
    }
}
Run Code Online (Sandbox Code Playgroud)

您还需要一个LoginAction显示和处理登录页面以及LogoutAction使会话无效或清除的页面.

配置

您需要将拦截器添加到堆栈中,并为"loginRedirect"创建全局结果映射.

<interceptors>
    <interceptor name="login" class="your.package.LoginInterceptor"/>

    <!-- sb: you need to configure all of your interceptors here. i'm only
         listing the one we created for this example. -->
    <interceptor-stack name="yourStack">
        ...
        <interceptor-ref name="login"/>
        ...
    </interceptor-stack>
</interceptors>

<global-results>
    <!-- sb: make this the path to your login action.
         this could also be a redirectAction type. -->
    <result name="loginRedirect" type="redirect">/login</url>
</global-results>
Run Code Online (Sandbox Code Playgroud)

  • 不,这不对.用户身份验证和授权通常是非常自定义的解决方案 (2认同)

Arv*_*ind 6

要维护会话,请在您的操作类中使用SessionAware接口并实现int public void setSession(Map m),它将把该属性作为映射中的键值对,可以从任何只需购买检索键的位置进行访问.

例如Action类

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class LogingEx extends ActionSupport implements SessionAware{
    private static final long serialVersionUID = 1L;

    private String stuname,stuage,country;
    private int stumarks;
    Map m;

    public String getStuname() {
        return stuname;
    }
    public void setStuname(String stuname) {
        this.stuname = stuname;
    }

    public String getStuage() {
        return stuage;
    }
    public void setStuage(String stuage) {
        this.stuage = stuage;
    }

    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }

    public int getStumarks() {
        return stumarks;
    }
    public void setStumarks(int stumarks) {
        this.stumarks = stumarks;
    }

    public void setSession(Map m)
    {
        this.m=m;
    }

    public String execute()
    {
        m.put("a",stuname);
        m.put("b", stuage);
        m.put("c",stumarks);
        m.put("d",country);

        return SUCCESS;
    }

}
Run Code Online (Sandbox Code Playgroud)

消息来源:http://www.java4s.com/struts-tutorials/example-on-struts-2-sessionaware-interface/