将数据从控制器显示到jsp页面

时间:2015-03-19 10:45:33

标签: java spring jsp spring-mvc

我看到很多示例如何显示数据,我将其定义并放入java控制器,但我无法做到。代码在这里。

@Controller
public class HomeController {
    @RequestMapping({"/","/test"})
    public String showHomePage(ModelMap model) {
        String mes = "Here I am";
        model.addAttribute("message",mes);
        return "new";
    }
}

new.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
${message}
</body>
</html>

当我启动jsp页面时,只显示此$ {message}

MVC-调度-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"
       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.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>
    <context:component-scan base-package="ru.sbt"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

1 个答案:

答案 0 :(得分:1)

不要直接点击jsp页面意味着不要将直接jsp名称放在url中而是将 / test (requestMapping url)调用到你的控制器然后控制器将发送带有模型的jsp (数据)到客户端请求

相关问题