Struts2没有使用带注释的操作

时间:2013-06-04 20:16:02

标签: java jsp struts2 annotations struts2-convention-plugin

我刚开始学习一些Struts2并尝试创建简单的网页。我创建了一个Action类,添加了一些注释,他们在web.xml文件中创建了一些JSP并设置了过滤器,但是当我尝试获取页面时,我只看到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;
    }
}

clientList.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>

的web.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>

URL:

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

结果:

Hello 

1 个答案:

答案 0 :(得分:1)

删除@ResultPath(value = "/")并设置location属性的绝对路径。同样在结果中,默认情况下使用注释name = "success",不必编写它。使用将struts2-convention插件添加到模块依赖项所需的注释配置。要执行操作,您需要输入URL

http://localhost:8080/client-list

如果您的模块已部署到根上下文,或在操作名称之前添加上下文路径。