简体   繁体   English

struts2程序无法正常工作

[英]struts2 program not working

I am new to struts and creating a basic application in struts2. 我是struts的新手,并在struts2中创建了一个基本应用程序。 But when i try to run the application, following error occured: HTTP Status 404 - description The requested resource () is not available 但是,当我尝试运行该应用程序时,发生以下错误:HTTP状态404-说明请求的资源()不可用

My directory is: HelloWorldStruts2 folder under webapps folder of tomcat. 我的目录是:tomcat的webapps文件夹下的HelloWorldStruts2文件夹。

in Project folder HelloWorldStruts2: WEB-INF(folder), HelloWorld.jsp, index.jsp 在Project文件夹HelloWorldStruts2中:WEB-INF(文件夹),HelloWorld.jsp,index.jsp

in WEB-INF folder: classes(folder), lib(folder),web.xml 在WEB-INF文件夹中:classes(文件夹),lib(文件夹),web.xml

in classes folder: com(folder)->tutorialspoint(folder)->struts2(folder)->HelloWorldAction(class file) struts.xml 在classes文件夹中:com(文件夹)-> tutorialspoint(文件夹)-> struts2(文件夹)-> HelloWorldAction(类文件)struts.xml

in lib folder: jar files: 在lib文件夹中:jar文件:

index.jsp 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 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 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 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 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>

please help 请帮忙

  1. Pack your project into a WAR, do not use exploded folder structure; 将项目打包到WAR中,不要使用分解的文件夹结构;

  2. Use the new Filter, StrutsPrepareAndExecuteFilter, instead of the deprecated FilterDispatcher; 使用新的过滤器StrutsPrepareAndExecuteFilter,而不使用不推荐使用的FilterDispatcher。

  3. Your DTD in web.xml, it is mixing 2.5 and 3.0 declarations; web.xml中的DTD,它混合了2.5和3.0声明; adjust it to the version supported by your Application Server (that you haven't specified here). 将其调整为Application Server支持的版本(此处未指定)。 Some AS use 2.4, other 2.5, other 3.0. 一些AS使用2.4,其他2.5使用3.0。

  4. Ensure you have all the needed JARs, all to the latest versions ( 2.3.16.3 for Struts, and other versions for OGNL and FREEMARKER). 确保您拥有所有必要的JAR文件,所有到最新版本( 2.3.16.3对Struts,和其他版本OGNL和Freemarker的)。 Remove unused JARs if present. 删除未使用的JAR(如果有)。

i suggest you to use this filter in your web.xml and remove whatever filter you're using, 我建议您在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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM