解决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页面:

 


               
                
               

 

 

情况一:报以下异常:

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配置过,否则会无法识别:


	

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

 

你可能感兴趣的:(struts2,action,jsp,传值交互)