当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Spring MVC - Model object not returning to view from controller- JSP

问题描述:

I'm writing a simple hello world program in spring mvc, where i'm passing an object to the view. The JSP page is opening properly, but its not getting the value which has been set in the controller and there are no errors in eclipse console. here is my code

@Controller

public class CncController {

String message = "Welcome to Spring MVC!";

@RequestMapping("/hello")

public ModelAndView showMessage(

@RequestParam(value = "name", required = false, defaultValue = "World") String name) {

System.out.println("in controller");

ModelAndView mv = new ModelAndView("helloworld");

mv.addObject("welcomeMessage", "Welcome to first page");

System.out.println(mv);

return mv;

}

}

cnc-dispatcher-servlet.xml

 <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:tx="http://www.springframework.org/schema/tx"

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.1.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<mvc:annotation-driven />

<bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>

<context:component-scan base-package="com.cnc.cnccontroller" />

<bean id="viewResolver"

class="org.springframework.web.servlet.view.InternalResourceViewResolver" >

<property name="prefix">

<value>/WEB-INF/</value>

</property>

<property name="suffix">

<value>.jsp</value>

</property>

</bean>

</beans>

The JSP page is put under the web-inf folder correctly..

It is just showing

Hello {welcomeMessage}

Is there anything missing in the flow?

网友答案:

Your JSP reference to the Model object should be within ${}. In your case, it should be "Hello ${welcomeMessage}"

分享给朋友:
您可能感兴趣的文章:
随机阅读: