1

I've googled this problem, but no one seem so the have exactly the same problem as me.

I'm trying to setup a simple Spring MVC application. These are the relevant files:

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

...

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>MyApp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>MyApp</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

myapp-servlet.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:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

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

HomeController.java

@Controller
public class HomeController 
{
    @RequestMapping({"/"})
    public String showHomePage(Map<String,Object> model)
    {
        return "home";
    }
}

I have a home.jsp in WEB-INF\views. The problem is my app isn't returning a home page. I just get a 404 and although Spring is finding my controller (the log says so) it's giving the error: no URL paths identified.

Can anyone see what I'm doing wrong?

Paul

2 Answers 2

2

Can you try with /app/* as the servlet mapping url and /home as the RequestMapping. Then try accessing it with /app/home. There are certain issues with mapping /* - once you get the rest of the mapping working with /app - we can look at removing the /app

Sign up to request clarification or add additional context in comments.

1 Comment

Hi gkamal, I had the same issue.. so after adding app it is working .. can u tell how to remove app now..?
0

I once wanted to complete a spring MVC project with rest URLs and I too could not find any really useful resources. I did post an article on my blog, but have since removed the blog. Here is an extract from the post:

I am using:

  • NetBeans 6.9.1
  • Spring Framework 3.0.2 RELEASE (including JSTL)
  • JAVA JDK 6
  • GlassFish Server 3
    • Start new NetBeans project
    • choose Java Web – Web Application
    • Click Next >
    • Enter Project Name "restMVC" and choose the project location
    • Click Next >
    • Choose GlassFish Server 3 – select Java EE 6 Web
    • Click Next >
    • Check Spring Web MVC – click Configuration tab and change Dispatcher Name to "restMVC" and Dispatcher Mapping to "/"
    • Click Finish
  1. Open restMVC-servlet.xml – we want to have the servlet search for the @Controller annotation within classes within a specific package, to do that we need to insert a context:component-scan into the file which means we also need to add the schemeLocation as follows:

    <?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:p="http://www.springframework.org/schema/p"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="
    
      http://www.springframework.org/schema/beans
    
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    <context:component-scan base-package="restMVC.web"/>
    ...
    

    Of course, we haven’t created the package yet, so let’s do that.

    • Right click on "Source Packages"
    • Go to New
    • Click Java Package
    • Enter the Package Name restMVC.web
  2. Now we can create our first controller –

    • Right click on the new restMVC.web package
    • Go to New
    • Click Java class
    • Enter the Class Name myController
  3. Next add the @Controller annotation, then right click – fix imports:

    package restMVC.web;
    import org.springframework.stereotype.Controller;
    
    @Controller
    public class myController {
    }
    
  4. Now we can add some request mapping URIs, remember we now have many different options for mapping using the @RequestMapping annotation, so be sure to review the Spring Docs.

    package restMVC.web;
    
    import java.util.ArrayList;
    import java.util.List;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class myController {
        @RequestMapping("/test")
        public String getTests(Model model) {
            List tests = new ArrayList();
            tests.add("<a href=\"test/1\">test 1</a>");
            tests.add("<a href=\"test/2\">test 2</a>");
            model.addAttribute("tests", tests);
            return "tests";
        }
        @RequestMapping("/test/{testId}")
        public String getTest(@PathVariable String testId, Model model) {
            List tests = new ArrayList();
            tests.add("test " + testId);
            model.addAttribute("tests", tests);
            return "tests";
        }
    }
    
  5. We need to create a view for tests.jsp. This is created by taking the String that is returned from the getTests and getTest methods ("tests") and using the viewResolver in the restMVC-servlet.xml appending .jsp – more on that to come.

    • Right click on (projects view) restMVC -> Web Pages -> WEB-INF -> jsp folder
    • Click New
    • Click JSP..
    • Enter File Name "tests"
  6. Edit tests.jsp. Enter the following at the top of the page:

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    

    And then this within the body element:

    <table>
      <c:forEach var="test" items="${tests}">
        <tr>
          <td>${test}</td>
        </tr>
      </c:forEach>
    </table>
    
  7. Finally we need to make sure the viewResolver is in restMVC-servlet.xml. The complete file should look like:

    <?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:p="http://www.springframework.org/schema/p"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd">
      <context:component-scan base-package="restMVC.web"/>
      <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/jsp/"
        p:suffix=".jsp" />
    </beans>
    
  8. You might want to edit redirect.jsp so it redirects to /test.

  9. Now click "Run Main Project"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.