欢迎使用CSDN-markdown编辑器

来源:互联网 发布:三星手表软件下载 编辑:程序博客网 时间:2024/06/06 03:12

如题
用struts2完成登陆拦截时
拦截器获取不到请求参数这是为什么啊。。
下面是代码:
拦截器

package Interceptor;import org.apache.struts2.ServletActionContext;import org.apache.struts2.dispatcher.Parameter;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class MyInterceptor extends AbstractInterceptor {      @Override      public String intercept(ActionInvocation invocation) throws Exception {        //1.获取参数         ActionContext ctx= invocation.getInvocationContext();         String name =(String)ctx.getSession().get("userName");         String pw =(String)ctx.getSession().get("userPw");         System.out.println(name);         System.out.println(pw);        //2.判断是否登录        if(name == null||pw == null){            System.out.println("用户没有登录");            return "error";        }        //3.用户登录        System.out.println("用户已经登录");        return invocation.invoke();    }}  

LoginAction

package login;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{    private String userName;    private String userPw;    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;    }    public String getUserPw() {        return userPw;    }    public void setUserPw(String userPw) {        this.userPw = userPw;    }    public String execute() throws Exception{              System.out.println("userName:"+userName);              System.out.println("userPw:"+userPw);              if(userName.equals("admin") && userPw.equals("admin123")){                return SUCCESS;            }            this.addFieldError("inputError", "输入错误,请重新输入");            return "error";}}

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@ taglib prefix="s" uri="/struts-tags" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><h1>登陆</h1><hr/><form action="login" method="post">账号:<input type="text" name="userName"><br/>密码:<input type="password" name="userPw"><br/><s:fielderror fieldName="inputError"></s:fielderror><br/><input type="submit" value="登陆"></form></body></html>

因为可以触发拦截器 就不发配置文件了
从下午3点多弄到现在还是没有搞懂怎么在拦截器中获取session的参数
求大神帮忙啊

原创粉丝点击