各位大牛,帮看一下诡异报错。替换变量名后,程序竟然不可用!

wyjlhf 2013-01-08 12:27:47
各位好。今天看《研磨strut2》中的关于直接使用域对象的数据对应方式,将书中习题代码简单实现了一下。发现一个问题。在Action类中,替换一个变量名,程序就会出现报错的现象。不知是为何问题。详细描述见下文:
实现的基本功能是,在登录页面login.jsp中输入账号密码,点击“登录”,跳转到welcomd.jsp页面。
其中 struts.xml 内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<constant name="struts.devMode" value="true" />
<constant name="struts.locale" value="zh_CN" />
<constant name="struts.il8n.encoding" value="UTF-8" />

<package name="helloworld" extends="struts-default">

<action name="helloworldAction" class="com.java.helloworld.struts2impl.action.HelloWorldAction">
<result name="toWelcome" >/s2impl/welcome.jsp</result>
<result name="input" >/s2impl/login.jsp</result>
</action>
</package>
</struts>



域对象类为HelloWorldModule.java 内容如下:
package com.java.helloworld.struts2impl.module;

public class HelloWorldModule {

private String account;
private String password;
private String submitFlag;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSubmitFlag() {
return submitFlag;
}
public void setSubmitFlag(String submitFlag) {
this.submitFlag = submitFlag;
}


}


Action为HelloWordAction.java 内容如下:
package com.java.helloworld.struts2impl.action;


import com.opensymphony.xwork2.ActionSupport;
import com.java.helloworld.struts2impl.module.*;

public class HelloWorldAction extends ActionSupport {

//使用属性模型(直接使用域对象)

private HelloWorldModule hwm = new HelloWorldModule();
public HelloWorldModule getHwm(){
return hwm;
}
public void setHwm(HelloWorldModule hwm){
this.hwm = hwm;
}


@Override
public String execute() throws Exception {

System.out.println("属性驱动(使用域对象输入参数) :account=" + hwm.getAccount() + " password= "
+ hwm.getPassword() + " submitFlag=" + hwm.getSubmitFlag());
return "toWelcome";
}

}


还有login.jsp 页面和welcome.jsp页面如下:
<%@ page language="java" import="java.util.*" 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>登录</title>
</head>
<body>




<form action="/HelloWorld/helloworld/helloworldAction.action" method="post">
<input type="hidden" name="hwm.submitFlag" value="login"/>
账号:<input type="text" name="hwm.account"/><br>
密码:<input type="password" name="hwm.password"><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>


<%@ page language="java" import="java.util.*" 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>
<%@ taglib prefix="s" uri="/struts-tags"%>
欢迎账号为<s:property value="hwm.account"/> 的朋友来访问!~

</body>
</html>


在Eclipse启动工程后,输入功能及跳转功能均正常,welcome.jsp出现的内容是:“欢迎账号为11111的朋友来访问!~”
后台日志打印如下:

2013-1-8 0:17:04 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Parameter [hwm.account] is not on the excludeParams list of patterns and will be appended to action!
2013-1-8 0:17:04 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Parameter [hwm.password] is not on the excludeParams list of patterns and will be appended to action!
2013-1-8 0:17:04 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Parameter [hwm.submitFlag] is not on the excludeParams list of patterns and will be appended to action!
属性驱动(使用域对象输入参数) :account=11111 password= 11111 submitFlag=login


但是问题出现了,当我把上述各文件中的“hwm”统一替换成hwmA后,再次重启Eclipse,重启输入点击登录后,welcome.jsp出现的内容是:“欢迎账号为 的朋友来访问!~” 后台打印日志为:

严重: Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'hwmA.password' on 'class com.java.helloworld.struts2impl.action.HelloWorldAction: Error setting expression 'hwmA.password' with value '[Ljava.lang.String;@1d532ae'
2013-1-8 0:22:38 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Error setting expression 'hwmA.submitFlag' with value '[Ljava.lang.String;@194df96'
ognl.OgnlException: target is null for setProperty(null, "submitFlag", [Ljava.lang.String;@194df96)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2326)
at ognl.ASTProperty.setValueBody(ASTProperty.java:127)
at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)
at ognl.SimpleNode.setValue(SimpleNode.java:301)
at ognl.ASTChain.setValueBody(ASTChain.java:227)
at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)

.......
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
2013-1-8 0:22:38 com.opensymphony.xwork2.util.logging.commons.CommonsLogger error
严重: Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'hwmA.submitFlag' on 'class com.java.helloworld.struts2impl.action.HelloWorldAction: Error setting expression 'hwmA.submitFlag' with value '[Ljava.lang.String;@194df96'
属性驱动(使用域对象输入参数) :account=null password= null submitFlag=null



严重: Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'hwmA.password' on 'class com.java.helloworld.struts2impl.action.HelloWorldAction: Error setting expression 'hwmA.password' with value '[Ljava.lang.String;@1d532ae'
属性驱动(使用域对象输入参数) :account=null password= null submitFlag=null[/code]

试过N次,始终不知道问题出在哪里。恳请各位有时间帮忙看一下,不胜感谢!
...全文
3123 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
IT_民工 2013-01-24
  • 打赏
  • 举报
回复
引用 5 楼 kowinner1 的回复:
看完你这个标题大概就能猜得到
怎么说?能具体点不?
IT_民工 2013-01-24
  • 打赏
  • 举报
回复
我新配置的项目也遇到了,是什么问题呢?在哪缺少了什么?
demohunter 2013-01-09
  • 打赏
  • 举报
回复
看完你这个标题大概就能猜得到
lwlwax 2013-01-09
  • 打赏
  • 举报
回复
肯定是有些地方少改了,建议LZ在Action类实现ModelDriven<HelloWorldModule>,这样不论是页面取值还是页面回显都会方便很多.
  • 打赏
  • 举报
回复
set struts.devMode to false。
少羽 2013-01-08
  • 打赏
  • 举报
回复
改变量名是没有问题的,要改的地方包括: 1:action类中的引用 2:action类中的set和get方法 3:jsp页面上的组件的name 改完这三个地方基本上就没问题了,你应该是少改了哪个地方。
wyjlhf 2013-01-08
  • 打赏
  • 举报
回复
引用 2 楼 fangmingshijie 的回复:
set struts.devMode to false。
刚按照你说的方式试了一下,不行呢。而且我理解,这个只是关闭开发模式,看不到报错而已吧
wyjlhf 2013-01-08
  • 打赏
  • 举报
回复
引用 1 楼 duanwu2330323 的回复:
改变量名是没有问题的,要改的地方包括: 1:action类中的引用 2:action类中的set和get方法 3:jsp页面上的组件的name 改完这三个地方基本上就没问题了,你应该是少改了哪个地方。
按照duanwu2330323朋友说的内容逐条检查了一下,发现确实少改了地方。如下所示 private HelloWorldModule hwmA = new HelloWorldModule(); public HelloWorldModule getHwm(){ return hwmA; } public void setHwm(HelloWorldModule hwmA){ this.hwmA = hwmA; } public void setHwm(HelloWorldModule hwmA){ this.hwmA = hwmA; } 上述代码是不正常的。我又把 getHwm()和setHwm(...) 方法名改成了如下形式,就可用了。看来还是自己太马虎了。谢谢duanwu2330323 了哈。 private HelloWorldModule hwmA = new HelloWorldModule(); public HelloWorldModule getHwmA(){ return hwmA; } public void setHwmA(HelloWorldModule hwmA){ this.hwmA = hwmA; }

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧