关于struts2,result的问题

wochu 2011-03-14 04:31:40
我使用一个拦截器验证用户是否登录,如果没有登录if(session.get("user")==null){return Action.LOGIN;},result的地址转向login(<result name="login">/login.jsp</result>,url地址是http://localhost:8080/struts2/struts/login.action。我现在的问题是:转到登录页面后为什么我在这个(http://localhost:8080/struts2/struts/login.action)页面上输入用户密码提交后,会出404错误。这是为什么?如何解决?
...全文
355 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wochu 2011-03-15
  • 打赏
  • 举报
回复
多谢了,问题我已经解决了
qm4050 2011-03-14
  • 打赏
  • 举报
回复
404,找不到资源。看一下你的路径有没有什么不对吧
Java 涛哥 2011-03-14
  • 打赏
  • 举报
回复
修改result:<result name="login" type="redirect">/login.jsp</result>
重定向跳转到login页面,那么你显示的地址就是http://localhost:8080/struts2/struts/login.jsp,你可以再尝试一下行不行,如果还不行的话可以将代码发给我,相互交流一下.
jeky_zhang2013 2011-03-14
  • 打赏
  • 举报
回复
有命名空间,你的页面也要有命名空间吧,可能是路径不一致
redlotus_lyn 2011-03-14
  • 打赏
  • 举报
回复
是因为命名空间错误导致的。

<s:form action="ActionAndInterceptor" namespace="/myinterceptor">
im110 2011-03-14
  • 打赏
  • 举报
回复
你的出错页面的地址栏中地址是什么?看看是不是路径的问题。
wochu 2011-03-14
  • 打赏
  • 举报
回复
拦截器
package myinterceptor;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import java.util.*;
public class MyInterceptor implements Interceptor{
public String intercept(ActionInvocation invocation)throws Exception{
Map<?,?> session=invocation.getInvocationContext().getSession();
if(session.get("user")==null){
return com.opensymphony.xwork2.Action.LOGIN;
}
return invocation.invoke();
}
public void init(){

}
public void destroy() {

}
}

Action代码:
package myinterceptor;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;
public class ActionAndInterceptor extends ActionSupport{
private String user;
private String password;
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute()throws Exception{
return SUCCESS;
}
public void validate(){
if(user.equals("myname")&password.equals("mypassword")){
ActionContext.getContext().getSession().put("user", user);
}else{
addFieldError("user",getText("login"));
}
}
}



package myinterceptor;
import com.opensymphony.xwork2.ActionSupport;
public class MyAction extends ActionSupport{
public String execute()throws Exception{
return SUCCESS;
}
}



struts.xml代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="m" namespace="/myinterceptor" extends="struts-default">
<interceptors >
<interceptor name="my" class="myinterceptor.MyInterceptor"/>
<interceptor-stack name="my">
<interceptor-ref name ="my" />
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors >
<action name="MyAction" class="myinterceptor.MyAction">
<interceptor-ref name="my"/>
<result name="success">/myinterceptor/MyJsp.jsp</result>
<result name="input">/index.jsp</result>
<result name="login">/index.jsp</result>
</action>
<action name="ActionAndInterceptor" class="myinterceptor.ActionAndInterceptor">
<result name="success">/myinterceptor/result.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
JSP:index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

</head>

<body>

<s:form action="myinterceptor/ActionAndInterceptor.action">
<s:textfield name="user" label="user"/>
<s:textfield name="password" label="password"/>
<s:submit/>
</s:form>
</body>
</html>

我首先进入index.jsp页面登录输入用户名和密码,可以运行MyAction 但是我先http://localhost:8080/struts2/myinterceptor/MyAction.action这个时,会显示index.jsp页面,然后输入用户名和密码点提交就会出错
gobelieveliu 2011-03-14
  • 打赏
  • 举报
回复
登陆的路径错误,lz把代码贴出来看看
wochu 2011-03-14
  • 打赏
  • 举报
回复
我如果先登录就不会出错,也就是先进登录页面登录,而不是去其他页面,让拦截器转向登录页面。这又是为什么呢?
wenwengo 2011-03-14
  • 打赏
  • 举报
回复
404就说明你的路径错了。。。

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧