解决struts2接收不到页面传递过来的对象VO的问题

本人最近利用struts2+ibatis+spring框架搭建web项目,需要写一个登录页面。众所周知,需要把JSP中输入的用户名、密码传递给后台action处理,判断用户名密码是否正确。其中会遇到很多action接收不到JSP页面传值而无法完成交互的问题,因此可以进行一一排查:

LoginAction:

private PortalUserVO portalUserVO;
	private UserService userService;
	private static final String LOGIN_VIEW = "login";
	private static final String LOGIN_SUCCESS = "success";
	private static final String LOGIN_FAILED = "login";
	
	public String doLoginView() {
		return LOGIN_VIEW;
	}
	
	public String doLogin() {
		String status = LOGIN_SUCCESS;
		String loginId = portalUserVO.getLoginId();
		String loginPasswd = portalUserVO.getPasswd();
		
		if(loginId == null || "".equals(loginId)) {
			errorMsg = "登录的用户名不能为空!";
			status = LOGIN_VIEW;
		}
		if(loginPasswd == null || "".equals(loginPasswd)) {
			errorMsg = "登录的用户密码不能为空!";
			status = LOGIN_VIEW;
		}
		
		try {
			PortalUserVO userVO = userService.getPortalUserByLoginId(portalUserVO.getLoginId());
			if(userVO == null) {
				errorMsg = "用户不存在,请检查输入!";
				status = LOGIN_FAILED;
			}else{
				if(!userVO.getPasswd().equals( portalUserVO.getPasswd())){
					errorMsg = "登录密码错误,请重新输入!";
					status = LOGIN_FAILED;
				}else{
					userService.updateUserLastLogin(portalUserVO);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return status;
	}

 jsp页面:

<s:form id="portalUserForm"  action="Login!login.action" onsubmit="return VerifyData(this)" theme="simple">
               
                <div class="email-div">
                  <label for="remember"><strong class="email-label">帐号</strong></label>
                  <input  type="text" spellcheck="false" name="portalUserVO.loginId" value="" id="loginId" >
                </div>
               <div class="passwd-div">
                  <label for="Passwd"><strong class="passwd-label">密码</strong></label>
                  <input  type="password" name="portalUserVO.passwd" value="" id="passwd">
                </div>
                <input name="signIn" type="submit" class="" id="signIn" value=""/>
             </s:form>

情况一:报以下异常:

java.lang.NullPointerException
	at com.hy.action.LoginAction.doLogin(LoginAction.java:21)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:404)
	at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:267)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:229)
	at com.hy.action.AuthenticationInterceptor.intercept(AuthenticationInterceptor.java:20)

  我检查了很久,后来通过debug才发觉action接收到页面的登录VO为null,发现是LoginAction没有portalUserVO的get/set方法,遂加上:

	public PortalUserVO getPortalUserVO() {
		return portalUserVO;
	}

	public void setPortalUserVO(PortalUserVO portalUserVO) {
		this.portalUserVO = portalUserVO;
	}

 错误解决。

情况二:页面的name属性name="portalUserVO.xxx"一定要与action中的PortalUserVO portalUserVO一致。

情况三:是否在alias.xml配置文件中为JSP页面的vo配置过,否则会无法识别:

<sqlMap namespace="alias">
<typeAlias alias = "portalUserVO" type= "xx.entites.PortalUserVO" />	
</sqlMap>

 同时,entites里同样得设置get/set方法(这好像是废话……)

猜你喜欢

转载自raising.iteye.com/blog/2166309