【问题标题】:No action mapped error while running Struts 2 app on Netbeans 7.3.1在 Netbeans 7.3.1 上运行 Struts 2 应用程序时没有操作映射错误
【发布时间】:2013-08-04 07:41:48
【问题描述】:

尝试构建一个 Struts 2 应用程序,它将用户引导到一个页面 (Display.jsp),该页面显示用户定义的 RGB 颜色配置的颜色。我从 Budi Karniawan 的 Struts 2 教程中获得了示例。当我手动剪切和粘贴源代码并将应用程序手动构建为 NB Web 应用程序时,尽管 RGB 参数以正确的格式输入,但它会引发验证错误,但它运行良好(我检查了我是否使用逗号分隔的数字输入RGB 坐标即:绿色为 0,255,0)。目录结构为:

然后我决定导入项目文件(从现有源选项创建 Web 应用程序)。我使用了 ant build.xml 文件来编译和运行应用程序。

当我通过应用名称运行应用时:

http://localhost:8084/Budi7c

我明白了:

no Action mapped for namespace [/] 

然后我附加struts.xml中映射的动作名称

http://localhost:8084/Budi7c/Design1.action

我得到一个 HTTP 404。但是当我尝试手动构建项目时,上面的 Deisgn1.action 参考有效。给定以下文件,谁能告诉我正确导入和运行此应用程序的最佳方法?我宁愿使用 ant 脚本而不是 MAVEN(因为使用 Maven 构建 Struts 2 似乎存在很多问题)。我只是想知道一种在尝试运行 struts 操作时避免 404 错误的方法。

如果我尝试手动构建应用程序,输入验证会失败(即使我输入了数字并用逗号分隔)。如果我尝试导入并使用 Ant 来确保正确构建,我最终会得到 404。

应用如下:

web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<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"> 

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



<!-- Restrict direct access to JSPs. 
For the security constraint to work, the auth-constraint
and login-config elements must be present -->
<security-constraint>
    <web-resource-collection>
        <web-resource-name>JSPs</web-resource-name>
        <url-pattern>/jsp/*</url-pattern>
    </web-resource-collection>
    <auth-constraint/>
</security-constraint>

<login-config>
    <auth-method>BASIC</auth-method>
</login-config>
</web-app> 

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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />

<package name="app07c" extends="struts-default">
    <action name="Design1">
        <result>/jsp/Design.jsp</result>
    </action>
    <action name="Design2" class="app07c.Design">
        <result name="input">/jsp/Design.jsp</result>
        <result name="success">/jsp/Display.jsp</result>
    </action>
</package>

</struts>

Color.java:

package app07c;
import com.opensymphony.xwork2.ActionSupport;

public class Color extends ActionSupport {
private int red;
private int green;
private int blue;
public int getBlue() {
    return blue;
}
public void setBlue(int blue) {
    this.blue = blue;
}
public int getGreen() {
    return green;
}
public void setGreen(int green) {
    this.green = green;
}
public int getRed() {
    return red;
}
public void setRed(int red) {
    this.red = red;
}
public String getHexCode() {
    return (red < 16? "0" : "") 
            + Integer.toHexString(red)
            + (green < 16? "0" : "")
            + Integer.toHexString(green) 
            + (blue < 16? "0" : "")
            + Integer.toHexString(blue);
}
}

Design.java:

package app07c;
import com.opensymphony.xwork2.ActionSupport;

public class Design extends ActionSupport {
private String designName;
private Color color;
public Color getColor() {
    return color;
}
public void setColor(Color color) {
    this.color = color;
}
public String getDesignName() {
    return designName;
}
public void setDesignName(String designName) {
    this.designName = designName;
}
}

MyColorConverter.java:

package app07c.converter;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
import app07c.Color;
import com.opensymphony.xwork2.conversion.TypeConversionException;

public class MyColorConverter extends StrutsTypeConverter {
public Object convertFromString(Map context, String[] values,
        Class toClass) {
    boolean ok = false;
    String rgb = values[0];
    String[] colorComponents = rgb.split(",");
    if (colorComponents != null 
            && colorComponents.length == 3) {
        String red = colorComponents[0];
        String green = colorComponents[1];
        String blue = colorComponents[2];
        int redCode = 0;
        int greenCode = 0;
        int blueCode = 0;
        try {
            redCode = Integer.parseInt(red.trim());
            greenCode = Integer.parseInt(green.trim());
            blueCode = Integer.parseInt(blue.trim());
            if (redCode >= 0 && redCode < 256 
                    && greenCode >= 0 && greenCode < 256 
                    && blueCode >= 0 && blueCode < 256) {
                Color color = new Color();
                color.setRed(redCode);
                color.setGreen(greenCode);
                color.setBlue(blueCode);
                ok = true;
                return color;
            }
        } catch (NumberFormatException e) {
        }
    }
    if (!ok) {
        throw new 
                TypeConversionException("Invalid color codes");
    }
    return null;
}

public String convertToString(Map context, Object o) {
    Color color = (Color) o;
    return color.getRed() + "," 
            + color.getGreen() + ","
            + color.getBlue();
}
}

Design.jsp:

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Color</title>
<style type="text/css">@import url(css/main.css);</style>
<style>
.errorMessage {
color:red;
}
</style>
</head>
<body>
<div id="global" style="width:450px">
<h4>Color</h4>
Please enter the RGB components, each of which is
an integer between 0 and 255 (inclusive). Separate two components
with a comma. For example, green is 0,255,0.
<s:form action="Design2">
    <s:textfield name="designName" label="Design Name"/>
    <s:textfield name="color" label="Color"/>
    <s:submit/>     
</s:form>

</div>
</body>
</html>

Display.jsp:

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Design Details</title>
<style type="text/css">@import url(css/main.css);</style>
<style type="text/css">
     .colorSample {
border:1px solid black;
width:100%;
height:100px;
background:#<s:property value="color.hexCode"/>;
}
</style>
</head>
<body>
<div id="global" style="width:250px">
<h4>Design details:</h4>
    Design name: <s:property value="designName"/>
    <br/>Color code: <s:property value="color"/>
    <div class="colorSample"/>
    </div>
    </body>
     </html>

我尝试将网络内容文件夹从/jsp 更改为/,以便项目结构与目录结构相同。然后我使用 ant 构建脚本来编译并运行项目并得到以下堆栈:

ant -f C:\\struts2\\budi_ebook\\struts2extractb\\app07c -DforceRedeploy=false     -Ddirectory.deployment.supported=true -Dnb.wait.for.caches=true run
init:
 deps-module-jar:
deps-ear-jar:
deps-jar:
Warning: Program Files (x86)\F-Secure\Anti-Virus\aquarius\fa.log modified in the future.
Warning: Program Files\CommVault\Simpana\Log Files\CVD.log modified in the future.
Warning: Users\ManaarDC\NTUSER.DAT modified in the future.
Warning: Users\ManaarDC\ntuser.dat.LOG1 modified in the future.
Warning: Users\RedGuard_Admin.MANAARNET\AppData\Local\Temp\3\output1375645810208 modified in     the future.
Warning: Users\RedGuard_Admin.MANAARNET\AppData\Local\Temp\3\toolbar_log.txt modified in the     future.
Warning: Windows\Temp\avg_secure_search.log modified in the future.
Warning: app\ManaarDC\diag\rdbms\orcldw\orcldw\trace\orcldw_dbrm_3148.trc modified in the future.
Warning: app\ManaarDC\diag\rdbms\orcldw\orcldw\trace\orcldw_dbrm_3148.trm modified in the future.
Warning: app\ManaarDC\product\11.2.0\dbhome_1\D5H9RBP1.ManaarNet.com_orclDW\sysman\emd\agntstmp.txt modified in the future.
 Warning: app\ManaarDC\product\11.2.0\dbhome_1\D5H9RBP1.ManaarNet.com_orclDW\sysman\log\emagent.trc modified in the future.
Warning: app\ManaarDC\product\11.2.0\dbhome_1\D5H9RBP1.ManaarNet.com_orclDW\sysman\log\emoms.log modified in the future.
Warning: app\ManaarDC\product\11.2.0\dbhome_1\D5H9RBP1.ManaarNet.com_orclDW\sysman\log\emoms.trc modified in the future.    
Warning: app\ManaarDC\product\11.2.0\dbhome_1\oc4j\j2ee\OC4J_DBConsole_D5H9RBP1.ManaarNet.com_orclDW\log\em-application.log modified in the future.
Warning: inetpub\logs\LogFiles\W3SVC1\u_ex130804.log modified in the future.
C:\struts2\budi_ebook\struts2extractb\app07c\nbproject\build-impl.xml:841: 
java.lang.OutOfMemoryError: GC overhead limit exceeded
at java.util.Arrays.copyOfRange(Arrays.java:2694)
at java.lang.String.<init>(String.java:203)
at java.lang.String.substring(String.java:1913)
at java.util.StringTokenizer.nextToken(StringTokenizer.java:352)
at org.apache.tools.ant.util.FileUtils.normalize(FileUtils.java:741)
at org.apache.tools.ant.util.FileUtils.resolveFile(FileUtils.java:616)
at org.apache.tools.ant.types.resources.FileResource.<init>(FileResource.java:60)
at org.apache.tools.ant.util.SourceFileScanner$1.<init>(SourceFileScanner.java:96)
at org.apache.tools.ant.util.SourceFileScanner.restrict(SourceFileScanner.java:95)
at org.apache.tools.ant.taskdefs.Copy.buildMap(Copy.java:787)
at org.apache.tools.ant.taskdefs.Copy.scan(Copy.java:744)
at org.apache.tools.ant.taskdefs.Copy.iterateOverBaseDirs(Copy.java:666)
at org.apache.tools.ant.taskdefs.Copy.execute(Copy.java:563)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.GeneratedMethodAccessor90.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:392)
at org.apache.tools.ant.Target.performTasks(Target.java:413)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:283)
at org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:541)
at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:153)
  BUILD FAILED (total time: 12 minutes 5 seconds)

【问题讨论】:

  • 我总是使用 maven 在 Netbeans 中构建 struts2 项目...文件->新建项目->(类别窗格:选择 Maven),(项目窗格:选择 Web 应用程序),其余说明如下:@ 987654321@我想我可以在几分钟内建立一个空白项目...下载依赖项放在一边

标签: java jsp netbeans struts2 struts


【解决方案1】:

无法从您的项目资源管理器中看到 web 内容根 目录,因为它不是目录结构,而是一个项目结构。例如,如果您使用 maven,那么它应该是 [project root]/src/main/webapp。此目录应包含WEB-INF 文件夹。如果您在项目设置中将 web 内容根 文件夹设置为 /jsp,那么这是错误的,因为它会影响 JSP 和其他项目文件。您应该将其设置为 /。在这种情况下,project rootweb content root 将是相同的,或者在 project root 文件夹中创建一个新文件夹,例如 WebContent并在那里放置jspWEB-INF 和其他网络资源。将 web 内容根 项目设置设置为 /WebContent。然后你可以在结果映射中使用/jsp/

【讨论】:

  • 好的,我尝试上传尽可能准确的目录结构图像。请让我知道这是否可以。现在的问题是它没有运行。当我选择“运行”选项时,它会冻结。 IDE 让我很困惑。我想我会受益于一本解释如何使用 Netbeans 构建 Struts2 的好书。使用 Maven 我发现原型存在一些问题,所以我不再确定构建 Struts2 的最佳方法(除了手动构建、创建战争文件和使用 Tomcat 管理器上传之外)。
  • 实际上,当我将网页内容根文件夹的设置更改为“/”时,它会运行但需要很长时间
  • 图像不可读,您能否发布更好的图像或发布格式为树的文本。 Netbeans has a maven plugin 我认为从 IDE 管理带有或不带有原型的 maven 项目没有任何困难。
  • 顺便说一句,如果您正在构建战争,那么即使使用 tomcat 管理器也将其部署到服务器,这是一个非常长的过程,您根本没有使用 IDE 部署功能和服务器配置、调试,您已经浪费了很多时间,您应该获得 IDE 技能以更好地管理项目。
  • 希望上图更清晰。关于 Maven,我很难使用 Eclipse 的 Maven 插件。原型产生了不正确的构建。我正在为 Netbeans 尝试 Maven。我已经阅读了 Netbeans 教程和几本关于构建 Struts2 和在 Netbeans 中使用 Maven 的书籍。这些指南往往与许多缺少配置细节的示例不一致。
【解决方案2】:

这就是我解决它的方法。我使用 Netbeans 'Web Applications with Existing Sources' 来导入项目。由于某种原因,导入的项目没有注册“jsp”目录。它只看到 Web Pages 目录中的 JSP 文件,而不是 Web Pages/jsp。所以我只是删除了struts.xml 中的/jsp 引用。该应用程序现在运行良好,验证错误不再存在。

我对这个答案很满意,因为我可以运行应用程序,但我不高兴我完全理解 IDE 如何构建这些类型的应用程序,因为导入的目录结构显然是错误的(并且错过了 jsp文件夹)。如果有人能进一步阐明这一点,或者我是否应该发布一个关于在 Netbeans 中构建 Struts2 的单独问题,将不胜感激

【讨论】:

  • 发布目录结构。
  • 对不起,我是新手。我可以将其作为图像文件发布吗?还是必须以文本形式发布?
  • this 示例。
猜你喜欢
  • 1970-01-01
  • 2019-10-16
  • 1970-01-01
  • 2016-07-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多