Spring变量没有进入视图

发布于 2024-10-31 21:22:40 字数 1404 浏览 0 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼藏柔 2024-11-07 21:22:40

尝试使用这样的结构:

<%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core" %>
...
<c:out value="${message}" />

您确定问题出在显示上吗?页面加载是否正常,我的意思是,除了“消息”之外,页面上是否显示所有其他 HTML?

编辑
尝试使用 org.springframework.web.servlet.ModelAndView 而不是
org.springframework.web.portlet.ModelAndView

Try to use such a construction:

<%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core" %>
...
<c:out value="${message}" />

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

金兰素衣 2024-11-07 21:22:40

您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文