0
votes

http://localhost:8080/AnchorMail/ works and goes to the index.jsp as it should.

http://localhost:8080/AnchorMail/greeting causes a HTTP status 404 not found.

It should be going to a hello JSP page, I believe. I'm new to this and any help would be so appreciated. Thank you.

the controller below

   @Controller
    public class HelloController {

        @RequestMapping(value="/greeting")
        public String sayHello(Model model) {

            model.addAttribute("greeting","Hello World");

            return "hello";
        }

}

servlet-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <mvc:annotation-driven/>
    <context:component-scan base-package="com.Controller"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property  name="prefix" value="/WEB-INF/jsp/"/>
        <property  name="suffix" value=".jsp"/>
    </bean>
</beans>

web.xml below

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

  <servlet>
    <servlet-name>anchorMailServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/servlet-config.xml</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>anchorMailServlet</servlet-name>
     <url-pattern>*.html</url-pattern>
  </servlet-mapping>

  <display-name>Archetype Created Web Application</display-name>

</web-app>

Pom.xml below

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>AnchorMail</groupId>
      <artifactId>AnchorMail</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>AnchorMail Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>provided</scope>
        </dependency>
      </dependencies>
      <build>
        <finalName>AnchorMail</finalName>
      </build>
    </project>
2
Do you have a hello.jsp file? What is the controller's package? - DwB
@DwB Hi, I have a controller package called com.Controller and that has the HelloController.java file. I also have a hello.jsp file in a folder called jsp in WEB-INF - Abdirizak Obsiye
What is the name of the war file you are installing into the JEE container? - DwB
I'm new to web development and i'm not sure what a JEE container is - Abdirizak Obsiye
How are you running the application? - DwB

2 Answers

0
votes

Look at this configuration element:

<servlet-mapping>
  <servlet-name>anchorMailServlet</servlet-name>
  <url-pattern>*.html</url-pattern>
</servlet-mapping>

Pay attention to the configuration that you created.

Requests that end in *.html are forwarded to your spring dispatcher servlet.

http://localhost:8080/AnchorMail/greeting does not end in .html

Also, try changing the url-pattern to /*.html.

0
votes
@RequestMapping(value="/greeting.html")

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property  name="prefix" value="/WEB-INF/jsp/"/> //other name then >>jsp
    <property  name="suffix" value=".jsp"/>
</bean>