Spring变量没有进入视图
我对 Spring 很陌生,似乎不明白这里出了什么问题。我确信它必须是简单的东西,但无论如何:
我正在浏览 此页面,我有几乎相同的代码。拆除 Apache Commons 库和 JSTL 内容后,我就开始做生意了。一切正常,实际上使用了我指定为视图的 jsp,但是在渲染站点时控制器中的变量“message”没有显示。这也不是因为表达式语言的问题,因为我也没有显示 ${message} 文本。这只是一个空白页。
我知道 jsp 实际上被触发的唯一原因是因为我在其中放置了一个标题,该标题在其他地方没有,并且它正在页面显示上使用。我还知道变量正在控制器中设置并且操作正在被调用,因为我在映射函数中放入了一个简单的系统输出。
感谢您的帮助!
编辑
这是jsp:
<%@page contentType="text/html" pageEncoding="UTF-8" isELIgnored="false"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>om nom JSP Page</title>
</head>
<body>
${message}
</body>
</html>
和控制器:
package org.me.home.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;
@Controller
public class SomeController {
@RequestMapping("/somepage")
public ModelAndView someAction() {
String mymsg = "Saying hi!";
System.out.println(mymsg);
return new ModelAndView("somepage", "message", mymsg);
}
}
I'm very new to Spring and can't seem to figure out what's wrong here. I'm sure it has to be something simple, but anyway:
I was going through the tutorial on this page, and I have nearly identical code. After pulling down the Apache Commons library and the JSTL stuff, I was in business. Everything works, down to it actually using the jsp I specified as the view, but the variable "message" in the controller is not being displayed when the site is rendered. It's not because of the expression language stuff, either, because I'm not getting the ${message} text displaying either. It's just a blank page.
The only reason I know that the jsp is actually being triggered is because I put a title in there that is no where else and it is being used on page display. I also know that the variable is being set in the controller and the action is being called because of a simple sysout that I put in the mapped function.
Thanks for any help!
EDIT
Here's the jsp:
<%@page contentType="text/html" pageEncoding="UTF-8" isELIgnored="false"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>om nom JSP Page</title>
</head>
<body>
${message}
</body>
</html>
And the controller:
package org.me.home.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;
@Controller
public class SomeController {
@RequestMapping("/somepage")
public ModelAndView someAction() {
String mymsg = "Saying hi!";
System.out.println(mymsg);
return new ModelAndView("somepage", "message", mymsg);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用这样的结构:
您确定问题出在显示上吗?页面加载是否正常,我的意思是,除了“消息”之外,页面上是否显示所有其他 HTML?
编辑
尝试使用 org.springframework.web.servlet.ModelAndView 而不是
org.springframework.web.portlet.ModelAndView
Try to use such a construction:
And are you sure, that the problem is in displaying? Is page loaded fine, I mean is all other HTML displayed on page, besides "message"?
EDIT
Try to use org.springframework.web.servlet.ModelAndView instead of
org.springframework.web.portlet.ModelAndView
您的 tomcat 可能使用了错误的 jstl.jar。在 tomcat lib 文件夹下,检查 jstl.jar 是否与项目中使用的版本相同。
Your tomcat maybe using the wrong jstl.jar. Under your tomcat lib folder, check if your jstl.jar is the same version you are using with your project.