我的设置有问题,我的登录已通过验证但未调用执行函数
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>
登录.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>
最佳答案
来自Struts2 Spring Plugin documentation :
Normally, in struts.xml you specify the class for each Action. When using the default
SpringObjectFactory
, the framework will ask Spring to create the Action and wire up dependencies as specified by the default auto-wire behavior.
这意味着您不需要从操作中创建 Spring bean。
However, sometimes you might want the bean to be completely managed by Spring. This is useful, for example, if you wish to apply more complex AOP or Spring-enabled technologies, such as Acegi, to your beans. To do this, all you have to do is configure the bean in your Spring applicationContext.xml and then change the class attribute from your`Action in the struts.xml to use the bean name defined in Spring instead of the class name.
Struts2 本身为每个请求创建新的 Action 实例,所以 Action 不是单例。如果您创建了一个 Spring bean,请给它一个合适的 scope
(例如 scope="prototype"
),因为:
By default, a bean will be a
singleton
, unless the bean has a parent bean definition in which case it will inherit the parent's scope.
loginActionBean
示例声明:
<bean id="loginActionBean" class="some.package.LoginActionBean" scope="prototype" />
关于java - Struts2/Spring - 即使在通过验证后也不会调用执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36307962/