2015-03-19 65 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

相關問題