简体   繁体   English

setter和getter方法无法识别

[英]setter and getter method not recognized

I'm strut2 beginner and I'm testing my very first example of hello world. 我是strut2初学者,并且正在测试我的第一个hello world示例。 Here is my action class: 这是我的动作课:

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;
    }
}

Here is the first page: 这是第一页:

<%@ 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>

And the second page: 第二页:

<%@ 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>

And finally the configuration file: 最后是配置文件:

<?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>

The problem is when I press the button with my entered name, it shows the error: 问题是当我按下带有输入名称的按钮时,它显示错误:

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', ]

If I changed property 'myname' to 'name' in action class and also the corresponding jsp files, it runs well without errors. 如果我在操作类和相应的jsp文件中将属性“ myname”更改为“ name”,则它将运行良好且没有错误。 Please suggest a fix. 请提出解决方案。

just check your jar files . 只需检查您的jar文件。 i just copy paste you code in my project and it is running fine without any error 我只是复制粘贴代码到我的项目中,它运行正常,没有任何错误

Please follow the naming convention of camelCase in variables in your POJO class. 请在POJO类的变量中遵循camelCase的命名约定。 I find Struts2 very sensitive towards it . 我发现Struts2对它非常敏感。

I faced one issue : If you name a variable eMail then struts will not work for this.If rename it to elecMail then it will work . 我面临一个问题:如果您给变量eMail命名,则struts对此不起作用。如果将其重命名为elecMail,则它将起作用。

See the pattern : eMail will have setter and getter method as setEMail() and getEMail() ie two consecutive capital letters . 参见模式:eMail将具有setter和getter方法,如setEMail()和getEMail(),即两个连续的大写字母。 Struts may have some defect in finding setter method name . Struts在查找setter方法名称时可能有一些缺陷。

May be something similar you are facing. 可能与您面临的情况类似。

Finally, I found the cause of the issue. 最后,我找到了问题的原因。 After changing property name, method's names and jsp files, we have to restart the server. 更改属性名称,方法名称和jsp文件后,我们必须重新启动服务器。 Only refreshing the browser causes the problem. 仅刷新浏览器会导致此问题。 Extending ActionSuppport or not doesn't affect relate to the issue. 扩展ActionSuppport是否影响与该问题无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM