簡體   English   中英

Netbeans 731上的Struts2運行錯誤

[英]Struts2 Running error on Netbeans 731

嘗試構建一個Struts2應用程序,該應用程序將用戶定向到顯示用戶定義的RGB顏色配置的顏色的頁面( Display.jsp )。 我從Budi Karniawan的Struts2教程中獲得了示例。 當我手動剪切並粘貼源代碼並作為NB Web應用程序手動構建應用程序時,盡管RGB參數以正確的格式輸入,但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

我得到了HTTP404。但是,當我嘗試手動構建項目時,上面的Deisgn1.action參考起作用了。 鑒於以下文件,任何人都可以告訴我正確導入和運行此應用程序的最佳方法嗎? 我寧願使用一個ant腳本而不是“ MAVEN”(因為使用Maven構建Struts2似乎存在很多問題)。 我只想知道一種在嘗試運行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>

我試圖將Web目錄文件夾從/jsp更改為/以便項目結構與目錄結構相同。 然后,我使用ant build腳本來編譯和運行項目,並獲得以下堆棧:

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)

從項目瀏覽器中看不到Web內容根目錄,因為它不是目錄結構,而是項目結構。 例如,如果您使用maven,則它應該是[project root]/src/main/webapp 該目錄應包含WEB-INF文件夾。 如果在項目設置中將Web內容根文件夾設置為/jsp ,那么這是錯誤的,因為它會影響JSP和其他項目文件。 您應該將其設置為/ 在這種情況下, 項目根目錄Web內容根目錄將相同,或者在項目根文件夾中創建一個新文件夾,即WebContent然后在其中放置jspWEB-INF和其他Web資源。 Web內容根項目設置設置為/WebContent 然后,您可以在結果映射中使用/jsp/

好吧,這就是我解決的方法。 我使用Netbeans的“具有現有資源的Web應用程序”來導入項目。 由於某些原因,導入的項目未注冊“ jsp”目錄。 它只是在Web Pages目錄中看到JSP文件,而不在Web Pages / jsp中看到。 因此,我只是在struts.xml中刪除了/ jsp參考。 該應用程序現在可以正常運行,並且不再存在驗證錯誤。

我對可以運行應用程序的程度感到滿意,但是由於導入的目錄結構顯然是錯誤的(並且錯過了jsp文件夾),我完全理解IDE如何構建這類應用程序,對此我並不滿意。 如果有人可以進一步闡明這一點,或者如果我應該在Netbeans中構建Struts2的主題下發表另一個問題,將不勝感激。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM