Announcement Announcement Module
Collapse
No announcement yet.
How does the Spring Template project work? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How does the Spring Template project work?

    This is the first time I am creating a spring template project.
    Every time I run the project, I get a 404 error.

    The project's name is SampleSprings and the context root is set to 'accounts'.
    When I run the project the URL is "localhost:8080/accounts/

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

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    </web-app>

    servlet-context.xml

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

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
    <beansroperty name="prefix" value="/WEB-INF/views/" />
    <beansroperty name="suffix" value=".html" />
    </beans:bean>

    <context:component-scan base-package="com.ctc.accounts" />



    </beans:beans>

    HomeController.java

    package com.ctc.accounts;

    import java.util.Locale;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMap ping;
    import org.springframework.web.bind.annotation.RequestMet hod;

    /**
    * Handles requests for the application home page.
    */
    @Controller
    public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /**
    * Simply selects the home view to render by returning its name.
    */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
    logger.info("Welcome home! The client locale is {}.", locale);

    return "home";
    }

    }

    home.jsp

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    <%@ page session="false" %>
    <html>
    <head>
    <title>Account Form</title>
    </head>
    <body>
    <!-- <form:form name="ExpenseDetails" method="Post" action="ExpenseDetailsForm.html">
    <table id="Parent1_Table">
    <tbody>
    <tr>
    <td>
    <table id="Item1_Table1">
    <tbody>
    <tr>
    <td>Item</td>
    <td><input type="text" name="Item1" required /></td>
    </tr>
    <tr>
    <td>Expense in Rs</td>
    <td><input type="text" name="ItemValue1"></td>
    </tr>
    </tbody>
    </table>
    </td>
    <td>
    <table id="Item1_Table2">
    <tbody>
    <tr>
    <td><button name="Edit1">Edit</button></td>
    <td><button name="Duplicate1">Duplicate</button></td>
    <td><button name="Trash1">Trash</button></td>
    </tr>
    </tbody>
    </table>
    </td>
    </tr>
    </tbody>
    </table>
    </form:form> -->
    </body>
    </html>

    Now I have a few questions.
    1. How does the context root gets set to 'accounts' i.e., the third part of the package-name 'com.ctc.accounts'?
    2. Why am I getting this 404 error, what is the mistake in the code?

    I am using Springsource Tool Suite and Spring Template project.

    If anything more than the information provided is needed, please let me know?

  • #2
    Hey!

    So I guess that you created your project using the Spring Web MVC template project in STS and deployed it into the tc Server that comes with STS, right?

    In that case, the web context is configured via a preference in the project. You can see (and change) that using the editor for the tc Server. Just double click on the server instance in the servers view and switch to the modules tab. There you see the "path" that the application is using.

    In addition to that the same setting can be configured using the preferences of the project, in the "web project settings" area.

    HTH,
    Martin

    Comment


    • #3
      Martin,

      Thank you very much for your help, I tried changing the context root through both ways. Now the URL is good and as I wanted.

      But still I am getting 404 error.
      Can you please spot the mistake that is giving the error?

      Yes you are correct, I am using the Spring Web MVC template project in STS and deployed it into the tc Server that comes with STS.

      Thanks

      Comment


      • #4
        Hey!

        Does the main tc Server page work? (http://localhost:8080)

        -Martin

        Comment


        • #5
          Martin,

          yes it does.

          Thanks

          Comment


          • #6
            Martin,

            Yes it does

            Thanks

            Comment


            • #7
              Hey!

              Ok, do you see any error output in the console output from the server?
              And did you try to do a clean build of the project and then de-add it to the server?

              I guess you are running a full STS distribution. Otherwise you would need to make sure that the Maven Integration for Eclipse and its extension for WTP are installed. Just checking...

              -Martin

              Comment


              • #8
                There is no error in the console output. Yes I cleaned and built before running the project.
                I don't understand what you mean by "Maven Integration for Eclipse and its extension for WTP are installed".
                Anyway here is my pom.xml.

                pom.xml

                <?xml version="1.0" encoding="UTF-8"?>
                <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>com.ctc</groupId>
                <artifactId>accounts</artifactId>
                <name>SampleSprings</name>
                <packaging>war</packaging>
                <version>1.0.0-BUILD-SNAPSHOT</version>
                <properties>
                <java-version>1.6</java-version>
                <org.springframework-version>3.1.1.RELEASE</org.springframework-version>
                <org.aspectj-version>1.6.10</org.aspectj-version>
                <org.slf4j-version>1.6.6</org.slf4j-version>
                </properties>
                <dependencies>
                <!-- Spring -->
                <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${org.springframework-version}</version>
                <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                </exclusion>
                </exclusions>
                </dependency>
                <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${org.springframework-version}</version>
                </dependency>

                <!-- AspectJ -->
                <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>${org.aspectj-version}</version>
                </dependency>

                <!-- Logging -->
                <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${org.slf4j-version}</version>
                </dependency>
                <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>${org.slf4j-version}</version>
                <scope>runtime</scope>
                </dependency>
                <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${org.slf4j-version}</version>
                <scope>runtime</scope>
                </dependency>
                <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.15</version>
                <exclusions>
                <exclusion>
                <groupId>javax.mail</groupId>
                <artifactId>mail</artifactId>
                </exclusion>
                <exclusion>
                <groupId>javax.jms</groupId>
                <artifactId>jms</artifactId>
                </exclusion>
                <exclusion>
                <groupId>com.sun.jdmk</groupId>
                <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                <groupId>com.sun.jmx</groupId>
                <artifactId>jmxri</artifactId>
                </exclusion>
                </exclusions>
                <scope>runtime</scope>
                </dependency>

                <!-- @Inject -->
                <dependency>
                <groupId>javax.inject</groupId>
                <artifactId>javax.inject</artifactId>
                <version>1</version>
                </dependency>

                <!-- Servlet -->
                <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.5</version>
                <scope>provided</scope>
                </dependency>
                <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.1</version>
                <scope>provided</scope>
                </dependency>
                <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
                </dependency>

                <!-- Test -->
                <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.7</version>
                <scope>test</scope>
                </dependency>
                </dependencies>
                <build>
                <plugins>
                <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                <additionalProjectnatures>
                <projectnature>org.springframework.ide.eclipse.cor e.springnature</projectnature>
                </additionalProjectnatures>
                <additionalBuildcommands>
                <buildcommand>org.springframework.ide.eclipse.core .springbuilder</buildcommand>
                </additionalBuildcommands>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
                </configuration>
                </plugin>
                <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <compilerArgument>-Xlint:all</compilerArgument>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
                </configuration>
                </plugin>
                <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                <mainClass>org.test.int1.Main</mainClass>
                </configuration>
                </plugin>
                </plugins>
                </build>
                </project>


                This is the report I get on the browser:

                HTTP Status 404 - /SampleSprings/
                type Status report

                message /SampleSprings/

                description The requested resource is not available.

                Comment


                • #9
                  Hey!

                  Can you zip the project folder and attach the project here? I would like to take a look.
                  But you are using a full STS distribution or did you install STS components into an existing Eclipse?

                  Cheers,
                  -Martin

                  Comment


                  • #10
                    Attached my project folder.

                    Yes I am using the the Spring Source Tool Suite 3.1.0 release not integrated with Eclipse.

                    Comment


                    • #11
                      Hey!

                      Thanks for the attached project. There are two things you should change in order to make it work:

                      The servlet-context.xml file has a line:

                      Code:
                      <beans:property name="suffix" value=".html" />
                      You should change that to:

                      Code:
                      <beans:property name="suffix" value=".jsp" />
                      The other piece is in the web.xml file, there you have changed the servlet mapping somehow. You should reset it to:

                      Code:
                      	<servlet-mapping>
                      		<servlet-name>appServlet</servlet-name>
                      		<url-pattern>/</url-pattern>
                      	</servlet-mapping>
                      Since you don't have any HTML pages that you serve, but instead do everything via JSP files, you should not change those settings. The default Spring Web MVC project contains the right settings, I think.

                      HTH,
                      Martin

                      Comment


                      • #12
                        Martin,

                        Thank you the changes worked good on my project.
                        But it did another thing. I had another Spring Template Project in my STS. It was also a basic project similar to this one without any added pages or controllers. Now without changing anything in this project, it has started to work. The same 404 error was shown in that project also.

                        How is this possible? The changes I made were to the project that we were talking earlier not to this.

                        Is it something related to server's error. I don't understand. Did the server need any trigger?

                        Perplexed.

                        Comment


                        • #13
                          Hey!

                          I have no idea about your other project or why that started to work now. Usually it is a good choice to stop the server, remove all deployed apps from it, restart it, re-add the adds to it again to trigger a fresh deploy, and see if that changes anything. I have no insight into your concrete setting, but that is what I would do... :-)

                          HTH,
                          Martin

                          Comment

                          Working...
                          X