0

您好,我正在为 struts2.2.1 做约定 插件,使用 netbeans6.9 的示例,似乎有问题。我什至复制/粘贴所有内容,什么都没有。现在我得到:

“错误消息是 ${message} ”应该是

“错误信息是 Hello World”

我无法发布结构,但我按照教程将 jsp 放在 WEB-INF 下,但什么也没有

web.xml

<web-app>
<display-name>My Application</display-name>
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>hello-world.jsp</welcome-file>
</welcome-file-list>

我的行动:

package com.example.actions;
import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {
  private String message;

  public String getMessage() {
   return message;
  }

  public String execute() {
   if (System.currentTimeMillis() % 2 == 0) {
       message = "It's 0";
       return "zero";
   }

    message = "It's 1";
    return SUCCESS;
  }

}

你好世界.jsp

<html>
<body> 
The Error message is ${message}
</body>
</html>

我认为我不需要 strut.xml 文件。我真的很沮丧,真的很感激任何帮助,或者你不能用 struts2.2.1 做注释吗?

4

1 回答 1

0


您在 web.xml 中拥有欢迎文件的方式只会显示“错误消息是”。

您需要直接执行操作,以便获得初始值。你需要一个额外的 index.html 像这样:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title>Dummy Project</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <META HTTP-EQUIV="Refresh" CONTENT="0;URL=hello.action">
</head>

<body>
<p>Project is Loading ...</p>
</body>
</html> 

Index.html 应该是 web.xml 中的欢迎文件。你的 struts.xml 在这里:

<struts>
   <constant name="struts.devMode" value="true"/>
   <include file="struts-default.xml"/>
    <package name="foo" extends="struts-default">
        <action name="hello" class="com.example.action.HelloWorld">
            <result name="success">/hello-world.jsp</result>
        </action>
    </package>
</struts>

“你好”是你的行动。这里只需要一个“成功”的结果。操作运行后,您将被重定向到您的 hello-world.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        The Error message is ${message}
    </body>
</html>

这是您的 HelloWorld.java。Execute() 只返回成功。这应该足够了。

package com.example.action;
import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public HelloWorld() {
    }

    @Override
    public String execute() {
        if (System.currentTimeMillis() % 2 == 0)
            message = "It's 0";
        else 
            message = "It's 1";
        return SUCCESS;
    }
}

你当然可以对注释做同样的事情。完成这项工作应该很容易。我使用这个NetBeans Struts2 Support插件。享受...

科斯蒂斯

于 2010-11-14T02:36:33.177 回答