簡體   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