javascript
Spring MVC-视图解析器(View Resolverr)-内部资源视图解析器(Internal Resource View Resolver)示例(转载实践)...
以下內容翻譯自:https://www.tutorialspoint.com/springmvc/springmvc_internalresourceviewresolver.htm
說明:示例基于Spring MVC 4.1.6。
InternalResourceViewResolver用于將提供的URI解析為實際的URI。以下示例顯示如何使用Spring Web MVC框架使用InternalResourceViewResolver。InternalResourceViewResolver允許使用請求映射網頁。
package com.tutorialspoint;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.ui.ModelMap;@Controller @RequestMapping("/hello") public class HelloController{@RequestMapping(method = RequestMethod.GET)public String printHello(ModelMap model) {model.addAttribute("message", "Hello Spring MVC Framework!");return "hello";}}?
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/"/><property name="suffix" value=".jsp"/> </bean>例如,使用上面的配置,如果是URI
/hello被請求,DispatcherServlet將請求轉發到前綴+ view-name + suffix = /WEB-INF/jsp/hello.jsp。
首先,讓我們使用Eclipse IDE,并按照以下步驟使用Spring Web Framework開發基于動態窗體的Web應用程序:
| 1 | 創建一個名為TestWeb的項目,在一個包com.tutorialspoint下,如Spring MVC - Hello World Example章節所述。 |
| 2 | 在com.tutorialspoint包下創建一個Java類HelloController。 |
| 3 | 在jsp子文件夾下創建一個視圖文件hello.jsp。 |
| 4 | 最后一步是創建所有源和配置文件的內容并導出應用程序,如下所述。 |
HelloController.java
package com.tutorialspoint;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.ui.ModelMap;@Controller @RequestMapping("/hello") public class HelloController{@RequestMapping(method = RequestMethod.GET)public String printHello(ModelMap model) {model.addAttribute("message", "Hello Spring MVC Framework!");return "hello";}}TestWeb-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:component-scan base-package="com.tutorialspoint" /><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean></beans>hello.jsp
<%@ page contentType="text/html; charset=UTF-8" %> <html> <head> <title>Hello World</title> </head> <body><h2>${message}</h2> </body> </html>?完成創建源文件和配置文件后,導出應用程序。右鍵單擊應用程序并使用Export > WAR File選項,并將您的TestWeb.war文件保存在Tomcat的webapps文件夾中。
現在啟動您的Tomcat服務器,并確保您可以使用標準瀏覽器從webapps文件夾訪問其他網頁。現在嘗試訪問URL?http://localhost:8080/TestWeb/hello,如果您的Spring Web應用程序一切正常,您應該看到以下結果:
Maven示例:
https://github.com/easonjim/5_java_example/tree/master/springmvc/tutorialspoint/test1
?
轉載于:https://www.cnblogs.com/EasonJim/p/7500027.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Spring MVC-视图解析器(View Resolverr)-内部资源视图解析器(Internal Resource View Resolver)示例(转载实践)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 线性代数(六)正交性
- 下一篇: gradle idea java ssm