【问题标题】:Spring MVC - Model object not returning to view from controller- JSPSpring MVC - 模型对象未从控制器返回视图 - JSP
【发布时间】:2016-09-28 08:27:02
【问题描述】:

我正在 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}

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

【问题讨论】:

  • 添加你的web.xml和相关的jsp。
  • 改成 Hello ${welcomeMessage}
  • @Sam,超级……知道了。 :) 回答它,我会接受作为答案。

标签: java spring jsp spring-mvc


【解决方案1】:

您对模型对象的 JSP 引用应该在 ${} 内。在你的情况下,它应该是“你好${welcomeMessage}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    相关资源
    最近更新 更多