Struts2/Spring-即使通过验证后也不执行调用 [英] Struts2/Spring - execute is not being called even after passing validate
问题描述
我在设置时遇到麻烦,我的登录名已通过验证,但未调用execute函数
I'm having trouble with my setup, my login has passed the validation but execute function is not being called
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"
I can see only "4" being printed (meaning it passed the validation) but that's it, it doesn't go to "5"
编辑:添加了tiles.xml
代码段
<definition name="/login.tiles" extends="baseLayout">
<put-attribute name="body" value="/login.jsp" />
</definition>
推荐答案
通常,在struts.xml中,为每个Action指定类.当使用默认的
SpringObjectFactory
时,框架将要求Spring创建Action并按照默认的自动装配行为指定的方式装配依赖项.
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.
Meaning you don't need to create Spring beans out of your actions.
但是,有时您可能希望Spring完全管理该bean.例如,如果您希望将更复杂的AOP或启用Spring的技术(例如Acegi)应用于bean,这将很有用.为此,您所需要做的就是在Spring applicationContext.xml中配置bean,然后从struts.xml中的Action更改class属性,以使用Spring中定义的bean名称而不是类名称.>
Struts2本身会为每个请求创建新的操作实例,因此操作不是单例.如果您无法正常使用创建Spring Bean,请为其提供适当的scope
(例如scope="prototype"
),因为:
Struts2 itself creates new instance of action for each request, so actions are not singletons. If you create a Spring bean out of action then give it a proper scope
(e.g. scope="prototype"
), because:
默认情况下,bean将是
singleton
,除非该bean具有父bean定义,在这种情况下它将继承父bean的作用域.
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" />
这篇关于Struts2/Spring-即使通过验证后也不执行调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!