2013年9月4日星期三

struts2 converter reported nullpointer error

action code
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 code
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();
//只处理请求参数数组第一个数组元素,
//并将该字符串以英文逗号分割成两个字符串
String[] userValues = params[0].split(",");
//为User实例赋值
user.setName(userValues[0]);
user.setPass(userValues[1]);
//返回转换来的User实例
return user;
}
else if (toType == String.class )
{
//将需要转换的值强制类型转换为User实例
User user = (User) value;
return "<" + user.getName() + ","
+ user.getPass() + ">";
}
return null ;
}
}

jsp code
<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2012, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date:
--%>

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>局部类型转换器</title>
</head>
<body>
<h3>局部类型转换器</h3>
<s:form action="login">
<s:textfield name="user" label="用户"/>
<tr>
<td colspan="2"><s:submit value="转换" theme="simple"/>
<s:reset value="重填" theme="simple"/></td>
</tr>
</s:form>
</body>
</html>

properties Codes

user=org.crazyit.app.converter.UserConverter

------ Solution ------------------------------------- -------
your convertValue this method which can not come in ? user does not have a value ?
------ For reference only -------------------------------------- -
user has a value ah , that is submitted with the form sprinkle
------ For reference only ----------------------- ----------------
user has a value ah , that is sprinkled with form submission  

solved ? What are the reasons .

没有评论:

发表评论