日期:2014-05-20  浏览次数:20734 次

strut2拦截器,页面传值传不到后台,请高手帮忙,下面为代码!!!
struts.xml
-----------------------
<struts>
<package name="interceptor_1" namespace="/c" extends="struts-default">

<interceptors>
<interceptor name="interceptor1" class="com.interceptor1"/>

<interceptor-stack name="default">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="interceptor1"/>
</interceptor-stack>

</interceptors>
<global-results>
<result name="message">/message.jsp</result>
</global-results>

<action name="login" class="com.interceptoraction">

<default-interceptor-ref name="default"/>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>

--------------------------
interceptoraction.java
-----------------------

public class interceptoraction extends ActionSupport {

private String user;
private String pass;
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
@Override
public String execute() throws Exception {
System.out.println("123");
ActionContext.getContext().put("mess","ok");
return "message";
}

}
-----------------------
interceptor1.java
----------------------

public class interceptor1 implements Interceptor {

public void destroy() {
}

public void init() {
}

public String intercept(ActionInvocation invocation) throws Exception {
ActionContext ctx=invocation.getInvocationContext();
Map session=ctx.getSession();
String user =(String)session.get("user");
String pass=(String)session.get("pass");
System.out.println(user);
System.out.println(pass);
if("czp".equals(user)&&"123".equals(pass))
{
return invocation.invoke();
}
else
{
return "input";
}
}

-----------------------------
message.jsp
-----------------------------

<body>
  ${mess}
  </body>
-----------------------------
index.jsp
-----------------------------
<body>
  
  <s:form action="c/login" method="post">
  <s:textfield name="user" label="name"></s:textfield>
  <s:password name="pass" label="pass"></s:password>
  <s:submit></s:submit>
   
  </s:form>
  </body>


------解决方案--------------------
没有set user怎么会get到user的值;拦截器和action在同一个包下,拦截器会起作用吗
------解决方案--------------------
public String intercept(ActionInvocation invocation) throws Exception {
String pass = (String)ServletActionContext.getRequest().getAttribute("pass");
}