【问题标题】:There is no Action mapped for namespace [/] and action name [hello] associated with context path [/HelloWorldStrut2]没有为命名空间 [/] 和操作名称 [hello] 映射的操作与上下文路径 [/HelloWorldStrut2] 关联
【发布时间】:2019-02-25 06:24:41
【问题描述】:

我是 struts2 编程的新手,我正在尝试制作简单的 struts2 helloworld 应用程序,但遇到此错误请帮忙。

错误:没有为命名空间 [/] 和操作名称 [hello] 映射的操作与上下文路径 [/HelloWorldStrut2] 关联。

动作类:

package com.tutorialspoint.struts2;

import com.opensymphony.xwork2.Action;

public class HelloWorldAction implements Action {

    private String name;

    @Override
    public String execute() throws Exception {
        return "success";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

struts.xml

   <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
            "http://struts.apache.org/dtds/struts-2.5.dtd">
    <struts>
        <constant name="struts.devMode" value="true"></constant>
        <package name = "default" namespace="/" extends = "struts-default">
         <action name = "hello" class = "com.tutorialspoint.struts2.HelloWorldAction">
             <result name = "success">/HelloWorld.jsp</result>
          </action>
       </package>
    </struts>

调用jsp、index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>
<html>
   <head>
      <title>Hello World</title>
   </head>
   <body>
      <h1>Hello World From Struts2</h1>
      <form action ="hello">
         <label for = "name">Please enter your name</label><br/>
         <input type = "text" name = "name"/>
         <input type = "submit" value = "Say Hello"/>
      </form>
   </body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>HelloWorldStrut2</display-name>
    <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>

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

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

在 pom.xml 中,struts 2 的依赖项

<dependencies>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.5.2</version>
        </dependency>
    </dependencies>

【问题讨论】:

  • 你能把你的struts.xml和你的JSP也贴出来吗?
  • 请参阅How to Ask 页面。只是行动没有帮助;最重要的是 Web 应用程序设置和 S2 映射。我也会考虑在网上搜索这个,因为这是初学者的常见问题。
  • 请发布您的 struts.xml 和 web.xml
  • 我已经添加了我的 struts.xml、jsp 和 web.xml 请告诉我为什么我会遇到这个问题
  • 你需要更改表单动作标签

标签: struts2


【解决方案1】:

请确保项目中添加了struts-tag.tld。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>HelloWorldStrut2</display-name>
    <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>

   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
      <init-param>
            <param-name>config</param-name>
            <param-value>
              struts-default.xml,../struts.xml
            </param-value>
        </init-param>
   </filter>

   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
   <jsp-config>
        <taglib>
            <taglib-uri>/struts-tags</taglib-uri>
            <taglib-location>//WEB-INF/struts-tags.tld</taglib-location>
        </taglib>
    </jsp-config>
</web-app>

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>
<html>
   <head>
      <title>Hello World</title>
   </head>
   <body>
      <h1>Hello World From Struts2</h1>
      <s:form action="hello">
         <label for = "name">Please enter your name</label><br/>
         <input type = "text" name = "name"/>
         <s:submit id="submitId" value = "Say Hello"/>
      </s:form>
   </body>
</html>

HelloWorld.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>
<html>
   <head>
      <title>Hello World</title>
   </head>
   <body>
      <h1>Hello World From Struts2</h1>
      <s:property value="name"/>
   </body>
</html>

【讨论】:

    猜你喜欢
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-06
    • 2014-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多