我想问一下,在struts2中从action可以跳转到action否?我的代码如下:
---------------login.jsp 登录页:----------------
<s:form action="preLogin" method="post">
<s:textfield name="username" label="username">
</s:textfield>
<s:password name="password" label="password"></s:password>
<s:submit></s:submit>
</s:form>
----------------PreLoginAction.java -------------------
private String username;
private String password;
/*getter、setter方法省略*/
public String execute() throws Exception {
ServletActionContext.getRequest().getSession().setAttribute("user", "true");
return SUCCESS;
}
@Override
public void validate() {
if(!"hello".equals(username) && username.trim().equals("") && username==null ){
this.addFieldError("username", "username wrong!!!");
} else if(!"world".equals(password) && password.trim().equals(password) && password==null){
this.addFieldError("password", "password wrong!!!");
}
}
---------------------LoginAction.java-----------------------
public String execute() throws Exception {
return SUCCESS;
}
-------------------CheckLoginInterceptor.java------------------------
public String intercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("begin check login interceptor!");
// String result = actionInvocation.invoke();
//
// System.out.println("--------"+result+"--------");
// 检查Session中是否存在user
/**
* 如果结果是这样,那我该怎么想session中注入值?
*/
Map session = actionInvocation.getInvocationContext().getSession();
String username = (String) session.get("user");
if (username != null && username.length() > 0) {
// 存在的情况下进行后续操作。
System.out.println("already login!");
return actionInvocation.invoke();
} else {
// 否则终止后续操作,返回LOGIN
System.out.println("no login, forward login page!");
return Action.LOGIN;
}
}
------------------struts.xml------------------
<action name="preLogin" class="org.study.test.actions.PreLoginAction">
<result>/login.action</result>
<result name="login">/login.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
<action name="login" class="org.study.test.actions.LoginAction">
<result>/success.jsp</result>
<interceptor-ref name="checkLogin" />
<interceptor-ref name="defaultStack" />
</action>
-------------------下面阐述我的问题:-----------------------
当我直接访问http://localhost:8080/test/login.action的时候没错,但是我访问http://localhost:8080/test/login.jsp的时候,它
本来应该跳转到PreLoginAction然后跳转到LoginAction的,但是却出错了,
页面的错误如下:
HTTP Status 404 - /test/login.action
type Status report
message /test/login.action
description The requested resource (/test/login.action) is not available.
------------------控制台没有报错,有些警告,如下:-------------------------
2009-8-28 1:29:52 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Parsing configuration file [struts-default.xml]
2009-8-28 1:29:53 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Unable to locate configuration files of the name struts-plugin.xml, skipping
2009-8-28 1:29:53 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Parsing configuration file [struts-plugin.xml]
2009-8-28 1:29:53 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Parsing configuration file [struts.xml]
2009-8-28 1:30:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: No configuration found for the specified action: 'preLogin' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
2009-8-28 1:30:10 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: No configuration found for the specified action: 'preLogin' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
希望高手帮我解决下,谢谢!
---------------login.jsp 登录页:----------------
<s:form action="preLogin" method="post">
<s:textfield name="username" label="username">
</s:textfield>
<s:password name="password" label="password"></s:password>
<s:submit></s:submit>
</s:form>
----------------PreLoginAction.java -------------------
private String username;
private String password;
/*getter、setter方法省略*/
public String execute() throws Exception {
ServletActionContext.getRequest().getSession().setAttribute("user", "true");
return SUCCESS;
}
@Override
public void validate() {
if(!"hello".equals(username) && username.trim().equals("") && username==null ){
this.addFieldError("username", "username wrong!!!");
} else if(!"world".equals(password) && password.trim().equals(password) && password==null){
this.addFieldError("password", "password wrong!!!");
}
}
---------------------LoginAction.java-----------------------
public String execute() throws Exception {
return SUCCESS;
}
-------------------CheckLoginInterceptor.java------------------------
public String intercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("begin check login interceptor!");
// String result = actionInvocation.invoke();
//
// System.out.println("--------"+result+"--------");
// 检查Session中是否存在user
/**
* 如果结果是这样,那我该怎么想session中注入值?
*/
Map session = actionInvocation.getInvocationContext().getSession();
String username = (String) session.get("user");
if (username != null && username.length() > 0) {
// 存在的情况下进行后续操作。
System.out.println("already login!");
return actionInvocation.invoke();
} else {
// 否则终止后续操作,返回LOGIN
System.out.println("no login, forward login page!");
return Action.LOGIN;
}
}
------------------struts.xml------------------
<action name="preLogin" class="org.study.test.actions.PreLoginAction">
<result>/login.action</result>
<result name="login">/login.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
<action name="login" class="org.study.test.actions.LoginAction">
<result>/success.jsp</result>
<interceptor-ref name="checkLogin" />
<interceptor-ref name="defaultStack" />
</action>
-------------------下面阐述我的问题:-----------------------
当我直接访问http://localhost:8080/test/login.action的时候没错,但是我访问http://localhost:8080/test/login.jsp的时候,它
本来应该跳转到PreLoginAction然后跳转到LoginAction的,但是却出错了,
页面的错误如下:
HTTP Status 404 - /test/login.action
type Status report
message /test/login.action
description The requested resource (/test/login.action) is not available.
------------------控制台没有报错,有些警告,如下:-------------------------
2009-8-28 1:29:52 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Parsing configuration file [struts-default.xml]
2009-8-28 1:29:53 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Unable to locate configuration files of the name struts-plugin.xml, skipping
2009-8-28 1:29:53 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Parsing configuration file [struts-plugin.xml]
2009-8-28 1:29:53 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Parsing configuration file [struts.xml]
2009-8-28 1:30:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: No configuration found for the specified action: 'preLogin' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
2009-8-28 1:30:10 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: No configuration found for the specified action: 'preLogin' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
希望高手帮我解决下,谢谢!