Monday, May 21, 2012

dispatcherservlet does not wire model to view

I made a model from a controller and return to dispatcherservlet. It looks like there is no problem



in model since I double checked output by system.out.println. for viewname string, I double checked



an actual directory name which is "WEB-INF/views/hello.jsp".



but I believe dispatcher servlet does not adapt model to a view since browser does not displayed



a model value which should be displayed. It would be easier to understand what I am going through



if I put my code here. so... here is my code.



web.xml



<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/appServlet/spring-servlet.xml
</param-value>
</context-param>


spring-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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
>
<context:annotation-config/>
<bean name="/hello" class="com.spring.toby.HelloController"/>
<bean id="HelloSpring" class="com.spring.toby.HelloSpring"></bean>
</beans>


and controller java file



public class HelloController implements Controller{

@Autowired HelloSpring helloSpring;
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
String name = request.getParameter("name");
String msg = this.helloSpring.sayHello(name);
System.out.println(msg);
Map<String, Object> model = new HashMap<String, Object>();
model.put("msg", msg);
return new ModelAndView("WEB-INF/views/hello.jsp", model);
}
}


and bean file



public class HelloSpring {
public String sayHello(String name){
return "Hello " + name;
}
}


can anybody tell me what am I doing wrong? thanks in advance.





No comments:

Post a Comment