【发布时间】:2015-03-19 10:45:32
【问题描述】:
我看到很多如何显示数据的示例,我定义并放入 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-dispatcher-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>
【问题讨论】:
-
你必须直接点击控制器路径而不是 JSP 路径。
-
我不懂你
-
我猜@RohitJain 的意思不是访问/new.jsp,而是访问/test
-
没问题。我可以访问我的页面。我没有输入控制器的消息
-
@jenius 好吧,该消息仅由控制器添加到 ModelAttribute 中。您必须访问控制器,该控制器会将您重定向到 JSP 页面。尽管您可以访问您的 JSP 页面,但您不应该这样做。
标签: java spring jsp spring-mvc