-
Mar 30th, 2011, 06:40 AM
#1
[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.
-
Mar 30th, 2011, 07:42 AM
#2
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.
-
Mar 30th, 2011, 07:51 AM
#3
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'
-
Mar 30th, 2011, 07:57 AM
#4
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.
-
Mar 30th, 2011, 09:58 AM
#5
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?
-
Mar 30th, 2011, 10:04 AM
#6
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/".
-
Mar 31st, 2011, 03:08 AM
#7
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
-
Forum Rules