Results 1 to 7 of 7

Thread: I can't call the controller

  1. #1
    Join Date
    Mar 2011
    Posts
    4

    Default [SOLVED]I can't call the controller

    Hi,
    I'm beginning with Spring and I was trying a few.
    I can't make the call '<a href="createBook">Insert a book</a>' and therefore can't display the insert page.

    spring-mvc-servlet:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans....
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <mvc:annotation-driven />

    <bean class="Controller.BookController" scope="session"/>


    <bean id="viewResolver" class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlVi ew" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
    </bean>
    </beans>

    web.xml:
    ..
    <servlet>
    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>spring-mvc</servlet-name>
    <url-pattern>/action/*</url-pattern>
    </servlet-mapping>
    ...

    BookController:
    ..

    @RequestMapping(value = "/createBook", method = RequestMethod.GET)
    public String welcome(Model model) {
    model.addAttribute("book",new Book());
    return "/book/createBook";
    }
    ...

    view.jsp:
    <body>
    <p>
    <a href="createBook">Insert a book</a>
    </p>
    <fieldset>
    <legend>book online</legend>
    <c:forEach items="${bookList}" var="book">
    Nome: <c:out value="${book.nome}" /><br/><br/>
    Autore: <c:out value="${book.autore}" /><br/><br/>
    Anno: <c:out value="${book.dataDiUscita}" /><br/><br/>
    <hr/>
    </c:forEach>
    </fieldset>
    </body>

    can someone help me?
    thanks a lot
    Last edited by smartsr; Mar 31st, 2011 at 03:08 AM.

  2. #2

    Default

    Hi,

    You map the dispatcher servlet to /action/* , so it will capture only URLs that begin with [context-name]/action. Is this your case? Since you use a relative URL in the href, we can't know what the absolute URL is. You should either change the URL to match the dispatcher mapping or the other way around.
    Gabriel Axel
    Sparklix | Blog | Twitter | Github

  3. #3
    Join Date
    Mar 2011
    Posts
    4

    Default

    Hi,
    I tried to insert the URL as name-Servlet mapppig 'spring-mvc' but does not work.
    How do I invoke the method of the welcome controller?
    my context-path isn't 'action'

  4. #4

    Default

    According to the web.xml and controller code you posted, the URL that is mapped to the welcome() method should be "[context]/action/createBook" (assuming you didn't annotate the controller class with @RequestMapping and assigned it a value too. In this case you need to add this value before "/createBook" in the link URL).

    I recommend using Firebug to see exactly where the link URL gets you.
    Gabriel Axel
    Sparklix | Blog | Twitter | Github

  5. #5
    Join Date
    Mar 2011
    Posts
    4

    Default

    if I add /createBook to URL of jsp, always give me the same error.
    Also goes to the upper path. (localhost:8080/createBook and not localhost:8080/smartsr/createBook)!
    How do I call the controller from the jsp?

  6. #6

    Default

    Again, in web.xml you mapped /action/* to the dispatcher, so URLs outside of this pattern will not be intercepted by SpringMVC controllers. Either change the mapping in web.xml or change the link URL to start with "/action/".
    Gabriel Axel
    Sparklix | Blog | Twitter | Github

  7. #7
    Join Date
    Mar 2011
    Posts
    4

    Default

    Good, the problem is solved.
    Thanks a lot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •