Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring MVC problem

 
Terril Douglas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I have been reading SIA 2 and am currently looking at chapter 13.
I am running the example shown in the book and am getting an error.

Using Eclipse I have debugged the app and when the app reaches the controller it continously go back after I return the ModelAndView object.

here are my config files:

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

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>

springmvc-servlet.xml:

<bean name="/jsp/hello_world.jsp"
class="springmvc.web.HelloWorldController" />

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

My controller name is HelloWorldController and is working fine. I believe the problem is with the mappings but in the book it specifies that the mapping isn't needed because there is one used by default. Can any one help?

Terril Douglas
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what the book means is that if you don't return a value for the View String, then it will just go back to the same page it came from. It is the default mechanism.

Mark
 
Terril Douglas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Spritzler:
I think what the book means is that if you don't return a value for the View String, then it will just go back to the same page it came from. It is the default mechanism.

Mark


The view string is a property that goes in the Controller?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the return type from a method in a Controller can return a ModelAndView object, or a String (which represents the view to forward to with a ViewResolver), or just a Model object, in which the String for the view is not set. In the case where the View string is not returned, then the default in Spring MVC is to display the same page the request came from. Well, in most cases with the default ViewResolver it will take an incoming request like "http://com.you.site/somepage.htm" then hack off the beginning part and the extension to just get "somepage" use that as the View String, then with the View Resolver prepend and append string to basically send you back to where you were.

Mark
 
Terril Douglas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I understand what you are talking about and in my controller I did place the view string in the return. I will just post my Controller:

package springmvc.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
//import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloWorldController extends AbstractController {
public HelloWorldController(){}

public ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

String aMessage = "Hello World MVC!";
//String lo = null;
//ModelAndView modelAndView = new ModelAndView();

return new ModelAndView("hello_world","message",aMessage);
}
}
 
Terril Douglas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Mark I think the problem was that my app was going in a loop because the view was the same component that was used to refer to my bean name in the springmvc-server.xml file. So essentially when it returned from the controller the request then looked to use the view which was referring right back to the bean. Sorry for any inconvenience.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Terril Douglas:
Hey Mark I think the problem was that my app was going in a loop because the view was the same component that was used to refer to my bean name in the springmvc-server.xml file. So essentially when it returned from the controller the request then looked to use the view which was referring right back to the bean. Sorry for any inconvenience.


No inconvenience. That is why we are here, maybe to help, or maybe to help you find it yourself despite me.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic