一个一个简单的登陆认证,但是Action中的execute()方法就是不执行,导致无法把用户名放入到session域中,我的代码如下有没高手帮帮忙看下
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
System.out.println("上面"+username);
}
//登录控制方法
public String loginMethod() throws Exception {
System.out.println("放入map之前"+username);
ActionContext.getContext().getSession().put("username", username);
return "loginOK";
}
}
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
/**
* 登录拦截器
* @author as
*
*/
public class LoginInterceptor implements Interceptor{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void init() {
// TODO Auto-generated method stub
}
/**
* 框架调用
* 会传入一个代表拦截器链的对象或动作:ActionInvocation
* 即通过ActionInvocation对象可以进入下一个拦截器或Action
*/
public String intercept(ActionInvocation invocation) throws Exception {
//String username=(String) invocation.getInvocationContext().getSession().get("username");
String username = (String) ActionContext.getContext().getSession().get("username");
System.out.println(username);
if(username==null||username.trim().length()==0){
return "toLoginJsp";
}else{
return invocation.invoke();
}
}
}
分XML配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="interceptorPackage" extends="struts-default"
namespace="/">
<!-- 定义拦截器 -->
<interceptors>
<interceptor name="loginInterceptor" class="interceptor.LoginInterceptor"></interceptor>
</interceptors>
<action name="loginRequest" class="interceptor.UserAction" method="loginMethod">
<interceptor-ref name="defaultStack"/>
<result name="loginOK" type="redirect">
/loginOK.jsp
</result>
<result name="toLoginJsp" type="dispatcher">
/login.jsp
</result>
<interceptor-ref name="loginInterceptor" />
</action>
</package>
</struts>
总XML配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<include file="interceptor/interceptor-struts.xml"/>
</struts>
------解决方案--------------------
这都被你猜到了.....
