2
package com.tutorialspoint.struts2;

public class HelloWorldAction{
   private String name;

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

   public String getName() {
      return name;
   }

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

索引.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<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>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
   <package name="helloworld" extends="struts-default">

      <action name="hello" 
            class="com.tutorialspoint.struts2.HelloWorldAction" 
            method="execute">
            <result name="success">/HelloWorld.jsp</result>
      </action>
   </package>
</struts>

web.xml

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

   <display-name>Struts 2</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.FilterDispatcher
      </filter-class>
   </filter>

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

HelloWorld.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   Hello World, <s:property value="name"/>
</body>
</html>

我为 Struts 中的 hello World 示例编写的这段代码但我收到错误:

HTTP 状态 404 - 没有为命名空间 / 和操作名称索引映射操作。

我是 struts 的新手,并尝试了解 struts Mvc Framw 的工作,但我不知道这里正在做错在哪里缺少这个请帮助我如何修复它

4

4 回答 4

2
  1. Use the new filter

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

    instead of the old one

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

    unless you are using a very old version of Struts2 (2.0 for example).

  2. Your web.xml deployment descriptor is messed up: you are saying it is 3.0, then linking 2.5 xmlns:web.

    If you have a Servlet 3.0 Container (Java EE 6) use:

    <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_3_0.xsd"
                    version="3.0">
    

    If you have a Servlet 2.5 Container (Java EE 5) use:

    <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">
    
  3. Specify namespace in your package declaration, so when you will add new packages you won't have problems:

    <package name="helloworld" extends="struts-default" namespace="/" >
    
  4. No need to specify the method in Action config if it is execute(), and no need to specify the result if it is "success":

    <action name="hello" class="com.tutorialspoint.struts2.HelloWorldAction" >
        <result>/HelloWorld.jsp</result>
    </action>
    
  5. Better using HTML5 DOCTYPE and UTF-8 CharSet if possible.

于 2013-10-23T09:31:22.987 回答
0

对于仍然遇到这些问题的任何人,请在此处查看我的答案。

在挣扎了近一个小时后,这对我有所帮助。我一步一步地按照教程进行操作,但仍然出现错误。

让我知道它是否有帮助。快乐编码:-)

于 2015-02-02T01:59:53.340 回答
0

清理您的应用程序目录,但最重要的是我看到您缺少命名空间。

示例包条目是 enter code here

包名称是您提供的逻辑名称,但名称空间必须是“/”,或者如果有任何其他上下文,则该名称

于 2014-06-17T13:36:42.410 回答
0

在你Struts.xml给的

<package name="helloworld" extends="struts-default">改成之后试试

<package name="default" extends="struts-default">

我希望这个答案能解决问题......

于 2014-02-28T10:21:20.450 回答