2015-03-19 71 views
1

我看到了很多示例如何显示数据,这些数据是我定义并放入java控制器的,但我无法完成。代码在这里。将数据从控制器显示到jsp页面

@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页面只显示这样$ {文}

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> 
+0

您必须直接命中控制器路径而不是JSP路径。 – 2015-03-19 10:46:31

+0

我不明白你 – jenius 2015-03-19 10:47:21

+0

我猜@RohitJain的意思是不访问/new.jsp,但/ test – bsiamionau 2015-03-19 10:48:03

回答

1

不要直接点击jsp页面就意味着不要直接把jsp的名字放在url中,而要把/test(requestMapping url)即调用您的控制器,然后控制器将发送jsp与模型(数据)到客户端请求

+0

我不理解你。写在代码 – jenius 2015-03-19 17:17:55

+0

有什么问题你怎么从你的浏览器调用这个程序复制粘贴在这里你在浏览器中打的网址。 – 2015-03-19 17:19:30

+0

我已经在上面展示过了 – jenius 2015-03-19 17:35:53

相关问题