1

我刚开始学习一些 Struts2 并尝试创建简单的网页。我创建了一个Action类,添加了一些注释,我创建了一些 JSP 并在web.xml文件中设置了过滤器,但是当我尝试获取页面时,我只看到 JSP 的工作而不是任何 Struts2。

ClientListAction.java:

@Namespace("/")
@ResultPath(value = "/")
@Result(name = "success", location = "pages/clientList.jsp")
public class ClientListAction extends ActionSupport {

    private String username = "TEST";

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

客户端列表.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Test page Struts 2</title>
</head>
<body>
<h4>Hello <s:property value="username"/></h4>
</body>
</html>

网页.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

    <display-name>Clients Application</display-name>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

网址:

http://localhost:8080/pages/clientList.jsp

结果:

Hello 
4

1 回答 1

1

Remove @ResultPath(value = "/") and set the absolute path for location attribute. Also in the result annotation name = "success" is used by default, writing it isn't necessary. To use annotation configuration required to add struts2-convention plugin to the module dependencies. To execute action you need to enter url

http://localhost:8080/client-list

if your module is deployed to the root context, or add context path before the action name.

于 2013-06-04T21:11:17.160 回答