繁体   English   中英

Spring MVC-模型对象不从控制器返回视图-JSP

[英]Spring MVC - Model object not returning to view from controller- JSP

我正在Spring MVC中编写一个简单的hello world程序,在这里我将一个对象传递给视图。 JSP页面可以正常打开,但是无法获取在控制器中设置的值,并且Eclipse控制台中没有错误。 这是我的代码

@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>

JSP页面正确放置在web-inf文件夹下。

它只是显示

Hello {welcomeMessage}

流程中是否缺少任何内容?

您对Model对象的JSP引用应在${} 在您的情况下,应为“ Hello ${welcomeMessage}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM