我有一个动作,我试图登录用户. public class RegisteredUserAction extends ActionSupport implements SessionAware {.... public String login() throws Exception { DBLogic dBLogic = new DBLogic(); RegisteredUser user = null; try { user
public class RegisteredUserAction extends ActionSupport implements SessionAware { .. .. public String login() throws Exception { DBLogic dBLogic = new DBLogic(); RegisteredUser user = null; try { user = dBLogic.getRegisteredUser(getUserUsername(), getUserPassword()); } catch (CredentialException e) { addFieldError("userUsername", e.getMessage()); addActionError(e.getMessage()); return INPUT; } if (user != null) { session.put("user", user); return SUCCESS; } return ERROR; } }
如您所见,如果用户名或密码无效,我会抛出一个CredentialException,然后在我的fieldError中填充该例外消息.我甚至尝试添加动作错误.
通过一些建议和在线搜索,我设法使用分别存储和提取的RegisteredUserAction.action和MainAction.action中的注释使用Message Store Interceptor.
import org.apache.struts2.convention.annotation.InterceptorRef; import org.apache.struts2.convention.annotation.InterceptorRefs; @InterceptorRefs({ @InterceptorRef(value = "store", params = {"operationMode", "STORE"}), @InterceptorRef("defaultStack"),})
和
import org.apache.struts2.convention.annotation.InterceptorRefs; @InterceptorRefs({ @InterceptorRef(value = "store", params = {"operationMode", "RETRIEVE"}), @InterceptorRef("defaultStack"),})
这是相关的struts.xml文件内容:
<package name="pages" extends="struts-default" namespace="/"> <global-results> <result name="displayPage">/WEB-INF/template/page.jsp</result> <result name="input">/WEB-INF/template/page.jsp</result> </global-results> <action name="registrationPage" class="actions.MainAction" method="loadRegistrationPage"></action> <action name="loginPage" class="actions.MainAction" method="loadLoginPage"></action> </package> <package name="operations" extends="struts-default" namespace="/"> <action name="userLogin" class="actions.RegisteredUserAction" method="login"> <result type="redirect" name="input">loginPage.action</result> <result type="redirect" name="success">homePage.action</result> </action> </package>
我正在使用基于模板的方法,其中中间内容(在我的情况下是LoginPage.jsp)动态地包含在主页中.
我的page.jsp(包括mainContent):
<s:include value="../%{mainContent}.jsp" ></s:include>
我的login.jsp(包括在内):
<%@taglib prefix="s" uri="/struts-tags"%> <s:form action="userLogin" method="POST"> <s:textfield name="userUsername" />Error:<s:fielderror name="userUsername" /> <s:password name="userPassword" /> <s:submit /> </s:form>
重定向正常工作,并显示登录表单,但没有错误.
我甚至尝试在struts.xml中使用Message Store拦截器而没有运气.