1
votes

I am building my first struts2 app and am getting the error:

  • HTTP Status 404 - /Struts2Beginner/Input.jsp
  • type Status report - message /Struts2Beginner/Input.jsp description The requested resource is not available.
  • Apache Tomcat/7.0.64

There are a lot of posts on this problem and I think I eliminated 2 of the common problems:

  1. I am using StrutsPrepareAndExecuteFilter (instead of FilterDispatcher)
  2. I have both commons-lang-2.4.jar and commons-lang3-3.2.jar in my WEB-INF/lib

I am following the tutorial http://www.codejava.net/frameworks/struts/struts2-beginner-tutorial-eclipse-tomcat-xml?showall=&limitstart=

WEB-INF lib

  • commons-fileupload-1.2.2.jar
  • commons-io-2.0.1.jar
  • commons-lang-2.4.jar
  • commons-lang3-3.1.jar
  • commons-logging-1.1.1.jar
  • commons-logging-api-1.1.jar
  • freemarker-2.3.19.jar
  • javassist-3.11.0.GA.jar
  • ognl-3.0.6.jar
  • struts2-core-2.3.8.jar
  • xwork-core-2.3.8.jar

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>
    <package name="Struts2Beginner" extends="struts-default">
        <action name="calculateSumAction" class="net.codejava.struts.SumAction"
            method="calculate">
            <result name="success">Result.jsp</result>
            <result name="input">Input.jsp</result>
        </action>
    </package>
</struts>

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>Struts2Beginner</display-name>

    <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>
</web-app>

SumAction.java

package net.codejava.struts;

import com.opensymphony.xwork2.ActionSupport;

public class SumAction extends ActionSupport {
    private int x;
    private int y;
    private int sum;

    /**
     * The action method
     * @return name of view
     */
    public String calculate() {
        sum = x + y;
        return SUCCESS;
    }

    // setters and getters for x, y, and sum:

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getSum() {
        return sum;
    }

    public void setSum(int sum) {
        this.sum = sum;
    }
}

Input.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts2 beginner example application</title>
</head>
<body>
    <center>
        <h2>Calculate sum of two numbers</h2>
        <s:form action="calculateSumAction" method="post">
            <s:textfield name="x" size="10" label="Enter X" />
            <s:textfield name="y" size="10" label="Enter Y" />
            <s:submit value="Calculate" />
        </s:form>
    </center>
</body>
</html>

Result.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sum Result</title>
</head>
<body>
    Sum of <s:property value="x"/>
    and <s:property value="y"/>
    is:
    <s:property value="sum"/>
</body>
</html>

I have spent several hours trying to debug this with no luck so far.

1
how you hit the url,it is something like /Struts2Beginner/Input.jsp ?. have you tried with /Struts2Beginnersubash
I'm using localhost:8080/Struts2Beginner/Input.jsp. I tried /Struts2Beginner but that did not work.jkfairless
I'm still getting 404 with localhost:8080/Struts2Beginner. Thanks!jkfairless
You shouldn't access jsp-s directly.Aleksandr M

1 Answers

0
votes

404 error will be faced when there is no welcome-file-list in the web.xml.

Add below piece of code in web.xml,

<welcome-file-list>
  <welcome-file>Input.jsp</welcome-file></welcome-file-list>

And run project by right click -> Run as -> Run on Server. and let us know if you are able to run the application. i have tried this program in my local tomcat server and its working fine.