2015-11-04 144 views
1

我是strut2初学者,我正在测试我的第一个Hello world例子。这里是我的动作类:setter和getter方法不被识别

package com.tutorialspoint.struts2; 

public class HelloWorldAction { 
    private String myname = ""; 

    public String execute() throws Exception { 
     System.out.println("Execute successfully"); 
     return "success"; 
    } 

    public String getMyname() { 
     return this.myname; 
    } 

    public void setMyname(String name) { 
     System.out.println("myName is set"); 
     this.myname = name; 
    } 
} 

这里是第一页:

<%@ 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>Hello World</title> 
</head> 
<body> 
<h1>Hello World From Struts2</h1> 
    <form action="hello"> 
     <label for="myName">Please enter your name</label><br/> 
     <input type="text" name="myname" value="No name"/> 
     <input type="submit"/> 
    </form> 
</body> 
</html> 

,第二页:

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<%@ taglib prefix="s" uri="/struts-tags" %> 
<!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>Hello World</title> 
</head> 
<body> 
    Hello World, <s:property value="myname"/> 
</body> 
</html> 

最后的配置文件:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
    "http://struts.apache.org/dtds/struts-2.0.dtd"> 
<struts> 
<constant name="struts.devMode" value="true" /> 
    <package name="helloworld" extends="struts-default"> 

     <action name="hello" 
      class="com.tutorialspoint.struts2.HelloWorldAction" 
      method="execute"> 
      <result name="success">/HelloWorld.jsp</result> 
     </action> 
    </package> 
</struts> 

问题是当我按下butto时n,其中我输入的名称,它显示了错误:

com.opensymphony.xwork2.interceptor.ParametersInterceptor error 
Unexpected Exception caught setting 'myname' on 'class com.tutorialspoint.struts2.HelloWorldAction: Error setting expression 'myname' with value ['No name', ] 

如果我在行动类改变属性“MYNAME”到“名”以及相应的jsp文件,它运行良好,无差错。 请提出修复建议。

+0

它显示相同的错误 – lenhhoxung

回答

1

只是检查你的jar文件。 我只是复制粘贴你的代码在我的项目中,它运行良好没有任何错误

+1

不是一个真正的答案,更像是一个评论。 –

1

请按照您的POJO类中变量的驼峰命名约定。我发现Struts2对它非常敏感。

我遇到了一个问题:如果你命名一个变量电子邮件,那么struts将无法工作。如果将它重命名为elecMail,那么它将工作。

查看模式:电子邮件将setter和getter方法设置为setEMail()和getEMail(),即两个连续的大写字母。 Struts在查找setter方法名称时可能会有一些缺陷。

可能是类似的东西你正面临着。

+0

感谢您的回答。我一直认为,直到我重新启动tomcat服务器。其实这个问题来自于不重启服务器。我是初学者,我想我已经学到了一些东西。 – lenhhoxung

+1

S2不符合驼峰惯例。单个第一个较低字符的情况是由于如何生成访问器。 –

0

最后,我找到了问题的原因。更改属性名称,方法名称和jsp文件后,我们必须重新启动服务器。只刷新浏览器会导致问题。扩展ActionSuppport或不影响与问题有关。