关于springMVC传值的一个小问题

dpj2008 dpj2008 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
最近在自学springMVC,搭建好环境之后遇到了一个通过ModelAndVIew传值的问题,在本已经测试过了,可以进入Controller类,且debug后ModelAndVIew中的msg有值,但是,在jsp取出的值却为空,代码如下,请进来的大侠们帮忙解答一下,小弟不胜感激。。

springMVC-serlet.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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- 设置使用注解的类所在的jar包 -->
<context:annotation-config />
<mvc:annotation-driven />
<context:component-scan base-package="controller"></context:component-scan>
<!-- 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>

</beans>


indexController.java

package controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;

@Controller
public class indexController {

@RequestMapping(value = "/index")
public ModelAndView index(HttpServletRequest request){
int i = 0;
i = i + 1;
System.out.println("index controller");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg", "hello world");
modelAndView.setView("WEB-INF/jsp/index.jsp");
return modelAndView;

}


}


index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>springMVC</title>
</head>
<body>
${msg}
</body>
</html>


上面就是使用的代码,访问后可以打开jsp网页,但是${msg}为空,为什么啊?
...全文
评论 6
收藏 已收藏
分享
返回顶部
dpj2008
Web 开发 81,108 成员 341,721 内容