小编典典

Struts2 / Spring-即使通过验证后也不执行调用

jsp

我在设置时遇到问题,我的登录名已通过验证,但未调用execute函数

LoginAction.java:

@Override
public String execute() throws Exception {

    System.out.println("5");
    String username = blogUser.getUsername();
    String password = blogUser.getPassword();
    blogUser = blogUserService.getUserByLogin(username, password);
    System.out.println("6");
    sessionMap.put(Constants.SESSION_USERNAME, blogUser.getUsername());
    System.out.println("7");
    sessionMap.put(Constants.SESSION_USERID, blogUser.getUserId());
    System.out.println("return:success");
    return SUCCESS;
}

@Override
public void validate() {
    System.out.println("1");
    String username = blogUser.getUsername();
    String password = blogUser.getPassword();
    System.out.println("username:"+username + ", password:"+password);
    if (username == null & password == null) {
        System.out.println("22");
        addFieldError("blogUser.username","");
    } else if (username == null || password == null) {
        System.out.println("2");
        addFieldError("blogUser.username","Invalid Login");
    } else if (!blogUserService.checkLogin(username, password)) {
        System.out.println("3");
        addFieldError("blogUser.username","Invalid Login");
    }
    System.out.println("4");
}

public String postLogin() throws Exception {
    System.out.println("77");
    return LOGIN;
}

struts.xml:

    <action name="login" class="loginActionBean" >
        <result name="input" type="tiles">/login.tiles</result>
        <result name="none" type="tiles">/login.tiles</result>
        <result name="login" type="tiles">/login.tiles</result>
        <result name="success" type="redirectAction">postPreviewAction</result>
        <result name="error" type="tiles">/login.tiles</result>
    </action>

    <action name="doLogin" class="loginActionBean" method="postLogin">
        <result name="login" type="tiles">/login.tiles</result>
        <result name="input" type="redirectAction">login</result>
    </action>

login.jsp:

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<div>
    <h2>Users Login</h2>
    <s:form action="login" method="post">
        <s:textfield label="Username" name="blogUser.username" />
        <s:password label="Password" name="blogUser.password" />
        <s:submit value="Login" />
    </s:form>
</div>

我只能看到“ 4”被打印(意味着它已通过验证),仅此而已,它并没有转到“ 5”

编辑: 添加了tiles.xml代码段

<definition name="/login.tiles" extends="baseLayout">
    <put-attribute name="body" value="/login.jsp" />
</definition>

阅读 343

收藏
2020-06-10

共1个答案

小编典典

Struts2 Spring插件文档中

通常,在struts.xml中,为每个Action指定类。使用default时SpringObjectFactory,框架将要求Spring创建Action并按照默认的自动装配行为指定的方式装配依赖项。

这意味着您无需从动作中创建Spring bean。

但是,有时您可能希望Spring完全管理该bean。例如,如果您希望将更复杂的AOP或启用Spring的技术(例如Acegi)应用于bean,这将很有用。为此,您要做的就是在Spring
applicationContext.xml中配置bean,然后从struts.xml中的Action更改class属性,以使用Spring中定义的bean名称而不是类名称。

Struts2本身会为每个请求创建新的操作实例,因此操作不是单例。如果您无法正常使用创建Spring
Bean,请为其提供适当的名称scope(例如scope="prototype"),因为:

默认情况下,bean将是一个singleton,除非该bean有一个父bean定义,在这种情况下它将继承父bean的作用域。

loginActionBean声明例子:

<bean id="loginActionBean" class="some.package.LoginActionBean" scope="prototype" />
2020-06-10