各位大牛,帮看一下诡异报错。替换变量名后,程序竟然不可用!
本帖最后由 wyjlhf 于 2013-01-08 00:37:33 编辑 各位好。今天看《研磨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>
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;
}
}
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";
}
}
<%@ 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>
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
严重: 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