struts2不调用类型转换器的问题
这是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.UserConverterentity.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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你好,我也遇到同样的问题,属性名称对应好后依然无法调用,可以看看你的proterties文件吗?
一楼正解,3Q!