在跑struts2的时候有时候会出现上面的错,特别是新手,
这种情况是在struts2高级的POJO访问时候出现的s
警告: Error setting expression 'user.password' with value '[Ljava.lang.String;@1cb2dd1'
ognl.OgnlException: target is null for setProperty(null, "password", [Ljava.lang.String;@1cb2dd1)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2219)
跟上面差不多的异常,我刚开始也遇到这个比较纠结的问题,下面我来说说我是如何解决的吧,可能大家遇到的跟我的不一样。但是这中间有几个地方需要注意,不然就会出现上面的错。
1、前台jsp
[java] view plain copy print ?
- <body>
- <form action="<%= path%>/login.action" method="post">
- 用户名:<input type = "text" name = "user.username"><br/>
- 密 码:<input type = "text" name = "user.password"><br/>
- <input type = "submit" value = "login">
- </form>
- </body>
<body> <form action="<%= path%>/login.action" method="post"> 用户名:<input type = "text" name = "user.username"><br/> 密 码:<input type = "text" name = "user.password"><br/> <input type = "submit" value = "login"> </form> </body>
注意两个地方,一个是input里面的name要写成**.XX;这里的**就是你后面action中的一个类对象,小写就OK,我这里是一个user,还有就是这里是name=不是id=,如果是用id的话,好像是传不到action中去的。
2、action类
[java] view plain copy print ?
- public class Login extends ActionSupport {
- public User getUser() {
- return user;
- }
-
- public void setUser(User user) {
- this.user = user;
- }
-
- private User user = new User();
-
- @Override
- public String execute() throws Exception {
- System.out.println("username="+user.getUsername()+",password="+user.getPassword());
- return SUCCESS;
- }
-
- }
public class Login extends ActionSupport { public User getUser() { return user; } public void setUser(User user) { this.user = user; } private User user = new User(); @Override public String execute() throws Exception { System.out.println("username="+user.getUsername()+",password="+user.getPassword()); return SUCCESS; } }
user类
[c-sharp] view plain copy print ?
- public class User {
- private String username;
- private String password;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
-
-
- }
public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
action中的 user类必须得new,如果不new会报空指针(除非你已经配好spring,让spring给你做这件事)
再一个就是必须得有get/set方法,user的get/set方法。
struts.xm;
[java] view plain copy print ?
- <package name="login" extends="struts-default">
- <action name="login" class="com.ssh2.action.Login">
- <result name="success" type="redirect">/index.jsp</result>
- <result name="error" type = "redirect">/login.jsp</result>
- </action>
- </package>
<package name="login" extends="struts-default"> <action name="login" class="com.ssh2.action.Login"> <result name="success" type="redirect">/index.jsp</result> <result name="error" type = "redirect">/login.jsp</result> </action> </package>
注意上面几点就没问题了。