新人学习SpringMvc,按照网上的自己写一直不能输出ModelAndView的值,代码如下:
springannotation-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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Spring3.1 前的注解 HandlerMapping --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> <!-- Spring3.1 前的注解 HandlerAdapter --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> </bean> <!-- ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 处理器 --> <bean class="com.iflytek.controller.HelloWorldController" /> </beans>
HelloWorldController 代码:
package com.iflytek.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.portlet.ModelAndView; @Controller public class HelloWorldController { @RequestMapping(value = "/hello") public ModelAndView SayHello() { System.out.println("======开始执行SayHello"); ModelAndView mv = new ModelAndView(); mv.addObject("message", "Hello World!"); mv.setViewName("hello"); System.out.println("======结束执行SayHello"); return mv; } }
hello.jsp 代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Hello World</title> </head> <body> <c:catch var="errorInfo"> 消息:${message} </c:catch> <c:out value="${errorInfo.message }"></c:out> </body> </html>
Console中可以输出:
======开始执行SayHello
======结束执行SayHello
说明
SayHello 方法是执行了,可是在jsp中取不到message的值,请各位专家帮忙看下,在线等....
找到问题了,自己比较大意,ModelAndView是引用的org.springframework.web.portlet.ModelAndView;