【问题标题】:Struts2 not worked with annotated actionStruts2 不适用于带注释的动作
【发布时间】:2023-03-20 22:30:02
【问题描述】:

我刚开始学习一些 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;
    }
}

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>

网址:

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

结果:

Hello 

【问题讨论】:

  • 网址错误。您正在直接访问 jsp。您应该访问该操作。您的网址应该类似于:localhost:8080\myapp\clientListAction
  • 我试过了,但没用

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


【解决方案1】:

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

http://localhost:8080/client-list

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    • 2021-11-06
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多