3

This is my first project in Struts. And when I run this project I get the error:

"The requested resource is not available." 

This is what I have done:

I am using Struts 2.3.15. This is what my project structure is: project structure.

This is the index.jsp page:

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

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

This is web.xml file:

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

This is HelloWorld.jsp:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World</title> 
</head>
 <body>
   Hello World, <s:property value="name"/>
 </body>
</html>

This is HelloWorldAction.jsp:

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

And one more thing that I have done is I copy pasted all the libraries from extracted struts folder to the WEB-INF/lib folder.

What I am missing out and what's the problem here?

Roman C
  • 49,761
  • 33
  • 66
  • 176
user1788048
  • 265
  • 1
  • 8
  • 18

2 Answers2

1

I think I've got solution to your problem, you need add this to the config file struts.xml which should be on classpath when you run your web application.

<action name="index">
    <result>/index.jsp</result>
</action>

try

http://localhost:8080/HelloWorldStruts2/index.action
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • still getting this error:There is no Action mapped for namespace [/] and action name [] associated with context path [/HelloWorldStruts2]. description The requested resource is not available – user1788048 Jun 26 '13 at 14:26
  • really? did you forget to put struts.xml to the web-inf/classes? – Roman C Jun 26 '13 at 14:28
  • action name should be `index` this error doesn't use my answer settings. – Roman C Jun 26 '13 at 14:29
  • yes it's there only.Now i am getting error as resource not available. – user1788048 Jun 26 '13 at 14:32
  • 1
    then put resource to the webcontent folder and remove files from `web.xml`. – Roman C Jun 26 '13 at 14:34
  • 1
    I mean the resource is index.jsp. – Roman C Jun 26 '13 at 14:36
  • to get it working after changes you need to clean, rebuild, redeploy, restart. – Roman C Jun 26 '13 at 14:39
  • Finally it worked... Thanks a lot. By the way, i don't have any idea about what is maven and why is it used for?If i use maven then will i not face problem of dependencies? – user1788048 Jun 26 '13 at 14:41
  • 1
    maven dependencies are preconfigured by the software engineers and stored in the repository far away from your eyes. If you use maven you should not face the problem with *incorrect* dependencies. – Roman C Jun 26 '13 at 14:45
  • there's eclipse plugin for maven called m2eclipse or m2e, that have a UI that helps to simplify the maven tasks while configuring and running your project with maven. – Roman C Jun 26 '13 at 14:52
0

Please remove the stack trace, it is not an answer of your question, so you should NOT put it as an answer, the reason you got this error is, you have too many jars copied into your ´WEB-INF/lib´, just remove all the plugins you don't need, there is the similar problem: Similar Problem, it could also be the solution for you

Community
  • 1
  • 1
Jaiwo99
  • 9,687
  • 3
  • 36
  • 53
  • Now I have added only the required jars. But Now i am getting this error when i run the project:There is no Action mapped for namespace [/] and action name [] associated with context path [/HelloWorldStruts2] – user1788048 Jun 26 '13 at 13:42
  • @user1788048 try localhost:8080/HelloWorldStruts2/hello.action, btw, you should set a string to the var ´name´, so it can be shown on the page – Jaiwo99 Jun 26 '13 at 13:53
  • i was looking over here which has same error as that of me:http://stackoverflow.com/questions/8991597/struts-hello-world-example-there-is-no-action-mapped-for-namespace-and-act , i changed my struts.xml position as i am not using maaven, still it's giving the same error. And i tried the url you gave but it show's the webpage cannot be displayed – user1788048 Jun 26 '13 at 13:55
  • @user1788048 You'd better use maven to resolve dependencies issue if you can't manage them manually. Different versions of libraries might have incompatibility issues that leads to incorrect behavior and error prone. – Roman C Jun 26 '13 at 14:08
  • 1
    @user1788048 seems you need add /index.jsp to your struts.xml and call HelloWorldStruts2/ – Jaiwo99 Jun 26 '13 at 14:16