我的设置有问题,我的登录已通过验证,但执行功能未被调用
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>
答案 0 :(得分:1)
名为doLogin
的操作(应具有showLogin
的正确名称)是显示登录页面的操作。它不应该被验证,因为它总是会失败。您必须从操作配置
<result name="input" type="redirectAction">login</result>
应该从验证中排除操作方法。您可以将validation
拦截器配置为exclude this method,但另一种方法是在方法上添加@SkipValidation
注释。
@SkipValidation
public String showLogin() throws Exception {
System.out.println("77");
return LOGIN;
}
名为login
的操作有一些可以删除的冗余结果
<result name="none" type="tiles">/login.tiles</result>
<result name="login" type="tiles">/login.tiles</result>
<result name="error" type="tiles">/login.tiles</result>
注意:默认情况下,对动作类中的每个操作方法都会调用验证,除非它从验证中排除,或者没有配置validation
拦截器。
<action name="login" class="loginActionBean" >
<result name="input" type="tiles">/login.tiles</result>
<result type="redirectAction">postPreviewAction</result>
</action>
<action name="showLogin" class="loginActionBean" method="showLogin">
<result name="login" type="tiles">/login.tiles</result>
</action>
答案 1 :(得分:1)
来自Struts2 Spring Plugin documentation:
通常,在struts.xml中为每个Action指定类。使用默认的
SpringObjectFactory
时,框架将要求Spring创建Action并按照默认的自动线路行为指定连接依赖项。
意味着您不需要从行动中创建Spring bean。
但是,有时您可能希望bean完全由Spring管理。例如,如果您希望将更复杂的AOP或Spring启用的技术(如Acegi)应用于bean,这很有用。要做到这一点,您所要做的就是在Spring applicationContext.xml中配置bean,然后从struts.xml中的`Action更改class属性,以使用Spring中定义的bean名称而不是类名。
Struts2本身为每个请求创建新的操作实例,因此操作不是单例。如果你创建了一个不起作用的Spring bean,那就给它一个合适的scope
(例如scope="prototype"
),因为:
默认情况下,bean将是
singleton
,除非bean具有父bean定义,在这种情况下它将继承父的范围。
loginActionBean
示例声明:
<bean id="loginActionBean" class="some.package.LoginActionBean" scope="prototype" />