【问题标题】:setting up struts2 project in netbeans Maven在 netbeans Maven 中设置 struts2 项目
【发布时间】:2013-04-28 19:51:49
【问题描述】:

以下是我所做的。 1.下载并使用所有jar 2.在WEB-INF中构建web.xml

  <!--?xml version="1.0" encoding="UTF-8"?-->

<web-app id="WebApp_ID" version="3.0"
    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">

    <display-name>Basic Strust 2 Project setup on Glassfish 3.0.1</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>

然后在WEB-INF中设置struts.xml

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "/WEB-INF/classes/struts-2.1.7.dtd">

<struts>
    <!--
    You could also set the constatnts in the struts.properties file
    placed in the same directory as struts.xml
    -->
    <constant name="struts.devMode" value="true" />

    <package name="test" extends="struts-default" namespace="/">

        <!--
        If no class attribute is specified the framework will assume success and
        render the result index.jsp
        If no name value for the result node is specified the success value is the default
        -->
        <action name="help">
            <result>/help.jsp</result>
        </action>

    </package>

</struts>

并在根文件夹中构建简单的 help.jsp。

我构建了动作类,help.java

import com.opensymphony.xwork2.ActionSupport;

public class help extends ActionSupport{
    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
}

实际上我认为现在我不需要这个 java 代码。

当我运行它并进入 help.action 时,它只是说 404 找不到, HTTP 状态 404 - 没有为与上下文路径 [/131X] 关联的命名空间 [/] 和操作名称 [帮助] 映射的操作。

【问题讨论】:

  • 有例外吗?

标签: jsp struts2 action-mapping


【解决方案1】:

您必须指定将分配给解析您的 .jsp 文件的操作类。在这种情况下,help 类对此负责,因此您必须在 struts.xml 中的 action 标记中将其指定为 classattribute。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "/WEB-INF/classes/struts-2.1.7.dtd">

<struts>
    <!--
    You could also set the constatnts in the struts.properties file
    placed in the same directory as struts.xml
    -->
    <constant name="struts.devMode" value="true" />

    <package name="test" extends="struts-default" namespace="/">

        <!--
        If no class attribute is specified the framework will assume success and
        render the result index.jsp
        If no name value for the result node is specified the success value is the default
        -->
        <action name="help" class="package_name.help">
            <result>/help.jsp</result>
        </action>

    </package>

</struts>

其中package_name 是包含help 类的包的名称。

【讨论】:

  • 如果action标签中缺少class属性,你认为会发生什么?
  • 没有映射到解析.jsp文件的动作类,你得到HTTP状态404。
  • 不知道,谢谢。根据问题:也许它与用于访问help.jsp的路径有关。记住包名很重要,然后使用...test/help.action
猜你喜欢
  • 2013-10-07
  • 2019-05-01
  • 2014-11-19
  • 2011-02-23
  • 1970-01-01
  • 1970-01-01
  • 2011-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多