开发者

Geting 404 error when request is made from JSP

I am trying to learn Struts2, but I am unable to create a working hello world application. It's giving a 404 (page not found) error when request is made from JSP. I kept all the lib files in the lib folder, placed struts.xml inside classes folder, but I am still getting this error.

Here is the view:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hello World</title>
    </head>
    <body>
    <form action="HelloWorld">
    
    
    <input type="submit">
    </form>
    
    </body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
   开发者_JAVA技巧     <url-pattern>/*</url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

This is my struts.xml file

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="default" extends="struts-default">
        <action name="HelloWorld" class="vaannila.HelloWorld">
            <result name="SUCCESS">/success.jsp</result>
        </action>
    </package>
</struts>

This is the error:

HTTP Status 404 - /Testi/HelloWorld

type Status report

message /Testi/HelloWorld

description The requested resource (/Testi/HelloWorld) is not available.

Apache Tomcat/6.0.33


This is the same question that you posted in Struts2 : Using form action just a bit more verbose.

You're problem is in the form tag you are using. It's not a struts form tag and the action attribute in a struts form tag is handled differently in a action attribute in an HTML tag.

I think my answer in Struts2 : Using form action should work for you.

PS: if you haven't changed your action extension, you may be able to make this work by simply adding a .action onto the end of your standard HTML tag action:

<form action="HelloWorld.action">
    ...
</form>


I would suggest to create a new folder under tomcat.webapps folder and place all your files according to the standards and give a try if you are using any IDE.

Try debugging from scratch,

  • first see if you are able to hit localhost:8080 sucessfully,
  • then try hitting a servlet(change the web.xml accordingly) to see if you are able to go through.
  • Finally try hitting struts2 action. Make sure url-mapping you give is correct.

I think this should be a mapping issue.


Can you post the error log content? and about the structure of the project.404indicatng the your server to able to locate the requested resources this can be a issue of your mapping or may be t unable to find the mapping file in class path. you need to place struts.xml file in the class path so if you are using Eclipse just drop your struts.xml file inside src folder rest eclipse know how to handle this

Also the best way to learn struts2 is to download the sample applications from there official site

Example Applications:

this will help you to understand and learn struts2 better because

  1. Sample application will help you to understand what structure one need to have
  2. What all configuration files need to be placed and at what location
  3. What are all required dependencies for a simple hello world application


Add imported lib to Action class

ex:

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport{

  //....
  //....
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜