2014-11-25 130 views
0

我是struts的新手,在struts2中創建一個基本的應用程序。但是,當我嘗試運行應用程序時,出現下列錯誤: HTTP狀態404 - 描述所請求的資源()不可用struts2程序不能正常工作

我的目錄是:的tomcat的webapps目錄下 HelloWorldStruts2文件夾。

在項目文件夾中HelloWorldStruts2

: WEB-INF(文件夾),的helloWorld.jsp,index.jsp的

在WEB-INF文件夾: 類(文件夾),LIB(文件夾),web.xml中

在類文件夾: COM(文件夾) - > tutorialspoint(文件夾) - > struts2的(文件夾) - > HelloWorldAction(類文件) struts.xml的

在lib文件夾:jar文件:

的index.jsp

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

的helloWorld.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<%@ taglib prefix="s" uri="/struts-tags" %> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
Hello World, <s:property value="name"/> 
</body> 
</html> 

HelloWorldAction.java

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

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

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> 

請幫助

+0

你有沒有在堆棧跟蹤的錯誤? – 2014-11-25 18:25:33

+0

首先,由於各種原因,請不要使用舊版本的S2。其次,'struts.xml'應該在classpath的根目錄下。 – 2014-11-25 20:31:13

回答

0
  1. 收拾好你的項目到一個WAR,不要使用爆炸文件夾結構;

  2. 使用新的Filter,StrutsPrepareAndExecuteFilter而不是棄用的FilterDispatcher;

  3. 您的DTD在web.xml中,它混合了2.5和3.0聲明;將其調整爲您的應用服務器支持的版本(您在此未指定)。一些AS使用2.4,其他2.5,其他3.0。

  4. 確保您擁有所有必需的JAR,均爲最新版本(針對Struts的2.3.16.3以及針對OGNL和FREEMARKER的其他版本)。如果存在,請刪除未使用的JAR。

0

我建議你在你的web.xml使用此過濾器,並刪除您正在使用的任何過濾器,

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