日期:2014-05-18  浏览次数:20583 次

struts2 converter 报nullpointer错误
action代码
package org.crazyit.app.action;

import com.opensymphony.xwork2.Action;

import com.crazyit.app.dto.User;

/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> 
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */ 
public class LoginAction implements Action
{
private User user;
private String tip;

//user属性的setter和getter方法
public void setUser(User user)
{
this.user = user;
}
public User getUser()
{
return this.user;
}

//tip属性的setter和getter方法
public void setTip(String tip)
{
this.tip = tip;
}
public String getTip()
{
return this.tip;
}

public String execute() throws Exception
{
if (getUser().getName().equals("crazyit.org"))
{
setTip("登录成功!");
return SUCCESS;
}
else
{
setTip("登录失败!!");
return ERROR;
}
}
}


converter代码
package org.crazyit.app.converter;

import java.util.Map;
import ognl.DefaultTypeConverter;

import com.crazyit.app.dto.User;
/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> 
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class UserConverter extends DefaultTypeConverter 
{
//类型转换器必须重写convertValue方法,该方法需要完成双向转换
public Object convertValue(Map context
, Object value, Class toType)
{
//当需要将字符串向User类型转换时
if (toType == User.class )
{
//系统的请求参数是一个字符串数组
String[] params = (String[])value;
//创建一个User实例
User user = new User();
//只处理请求参数数组第一个数组元素,
//并将该字符串以英文逗号分割成两个字符串