关于springMVC传值的一个小问题

dpj2008 2015-12-03 02:47:39
最近在自学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}为空,为什么啊?
...全文
139 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
蓝鲸人 2015-12-04
  • 打赏
  • 举报
回复
引用 4 楼 s_xl1988 的回复:
[quote=引用 3 楼 s_xl1988 的回复:] [quote=引用 1 楼 jia20003 的回复:] 谁教你这么写的,照哪个人博客的/哪本书,都可以扔了!,只需如此即可
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String goHome(Locale locale, Model model) {
		logger.info("Admin page! The client locale is {}.");
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		String formattedDate = dateFormat.format(date);

		model.addAttribute("msg", formattedDate);
		return "index";
	}
谢谢了,确实是这里的问题[/quote] 引用错了。。还是很感谢你[/quote] 我靠这种跳转也行。。。亲测可用啊。。。又涨了一波见识
dpj2008 2015-12-03
  • 打赏
  • 举报
回复
引用 2 楼 yang362046076 的回复:
你controller那个类的包导入错了 import org.springframework.web.servlet.ModelAndView (正确应该引入这个) improt org.springframework.web.portlet.ModelAndView (这个是错误的)
谢谢,时报引用错误的问题
dpj2008 2015-12-03
  • 打赏
  • 举报
回复
引用 3 楼 s_xl1988 的回复:
[quote=引用 1 楼 jia20003 的回复:] 谁教你这么写的,照哪个人博客的/哪本书,都可以扔了!,只需如此即可
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String goHome(Locale locale, Model model) {
		logger.info("Admin page! The client locale is {}.");
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		String formattedDate = dateFormat.format(date);

		model.addAttribute("msg", formattedDate);
		return "index";
	}
谢谢了,确实是这里的问题[/quote] 引用错了。。还是很感谢你
dpj2008 2015-12-03
  • 打赏
  • 举报
回复
引用 1 楼 jia20003 的回复:
谁教你这么写的,照哪个人博客的/哪本书,都可以扔了!,只需如此即可
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String goHome(Locale locale, Model model) {
		logger.info("Admin page! The client locale is {}.");
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		String formattedDate = dateFormat.format(date);

		model.addAttribute("msg", formattedDate);
		return "index";
	}
谢谢了,确实是这里的问题
踩沙滩 2015-12-03
  • 打赏
  • 举报
回复
你controller那个类的包导入错了 import org.springframework.web.servlet.ModelAndView (正确应该引入这个) improt org.springframework.web.portlet.ModelAndView (这个是错误的)
gloomyfish 2015-12-03
  • 打赏
  • 举报
回复
谁教你这么写的,照哪个人博客的/哪本书,都可以扔了!,只需如此即可
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String goHome(Locale locale, Model model) {
		logger.info("Admin page! The client locale is {}.");
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		String formattedDate = dateFormat.format(date);

		model.addAttribute("msg", formattedDate);
		return "index";
	}

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧