struts2不调用类型转换器的问题

荒唐的云飞扬 发布于 2013/07/23 18:36
阅读 1K+
收藏 0

这是web项目结构图 

    问题是struts2根本不调用类型转换器,直接调用了execute()方法,结果报了空指针异常,我把关键配置的文件和代码放在靠上面,希望大神帮我解决,谢谢哈

                

以下是文件信息

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
	<filter-name>struts2</filter-name>
	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>struts2</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
src/struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
	"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="struts" extends="struts-default" namespace="/">
		<action name="userAction" class="action.UserAction">
			<result name="success">/test.jsp</result>
		</action>
	</package>
</struts>
converter.UserConverter
package converter;
import java.util.Map;
import entity.User;
import ognl.DefaultTypeConverter;
public class UserConverter extends DefaultTypeConverter {
	public Object convertValue(Map context, Object value, Class toType) {
		System.out.println("converter..");
		if(User.class == toType){
			User u=new User();
			String str=((String[])value)[0];
			String[] info=str.split(";");
			u.setUsername(info[0]);
			u.setPwd(info[1]);
			return u;
		}
		return null;
	}
}
action.UserAction
package action;
import com.opensymphony.xwork2.ActionSupport;
import entity.User;
public class UserAction extends ActionSupport{
	private User u;
	public User getU() {
		return u;
	}
	public void setU(User u) {
		this.u = u;
	}
	public String execute() throws Exception{
		System.out.println(u.getUsername());
		System.out.println(u.getPwd());
		return "success";
	}
}
action/UserAction-conversion.properties
u=converter.UserConverter
entity.User
package entity;
public class User {
	private String username;
	private String pwd;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
}
user.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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body style="font-size:30px;">
	<form action="userAction.action">
               用户名和密码用";"隔开<br/>
		username:<input type="text" name="username"><br/>
		<input type="submit" value="submit">
	</form>
</body>
</html>
test.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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body style="font-size:30px;">
	test..
</body>
</html>
加载中
0
赵旸
赵旸

首先看这里:user.jsp--<input type="text" name="username">

然后看这里:action.UserAction-- private User u;

应该是:<input type="text" name="u">

属性名称对应好才ok~

0
barte
barte
一楼正解,3Q!
Failure
Failure
你好,我也遇到同样的问题,属性名称对应好后依然无法调用,可以看看你的proterties文件吗?
返回顶部
顶部