javascript
Spring MVC-视图解析器(View Resolverr)-内部资源视图解析器(Internal Resource View Resolver)示例(转载实践)...
以下內(nèi)容翻譯自:https://www.tutorialspoint.com/springmvc/springmvc_internalresourceviewresolver.htm
說明:示例基于Spring MVC 4.1.6。
InternalResourceViewResolver用于將提供的URI解析為實(shí)際的URI。以下示例顯示如何使用Spring Web MVC框架使用InternalResourceViewResolver。InternalResourceViewResolver允許使用請(qǐng)求映射網(wǎng)頁。
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被請(qǐng)求,DispatcherServlet將請(qǐng)求轉(zhuǎn)發(fā)到前綴+ view-name + suffix = /WEB-INF/jsp/hello.jsp。
首先,讓我們使用Eclipse IDE,并按照以下步驟使用Spring Web Framework開發(fā)基于動(dòng)態(tài)窗體的Web應(yīng)用程序:
| 1 | 創(chuàng)建一個(gè)名為TestWeb的項(xiàng)目,在一個(gè)包c(diǎn)om.tutorialspoint下,如Spring MVC - Hello World Example章節(jié)所述。 |
| 2 | 在com.tutorialspoint包下創(chuàng)建一個(gè)Java類HelloController。 |
| 3 | 在jsp子文件夾下創(chuàng)建一個(gè)視圖文件hello.jsp。 |
| 4 | 最后一步是創(chuàng)建所有源和配置文件的內(nèi)容并導(dǎo)出應(yīng)用程序,如下所述。 |
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>?完成創(chuàng)建源文件和配置文件后,導(dǎo)出應(yīng)用程序。右鍵單擊應(yīng)用程序并使用Export > WAR File選項(xiàng),并將您的TestWeb.war文件保存在Tomcat的webapps文件夾中。
現(xiàn)在啟動(dòng)您的Tomcat服務(wù)器,并確保您可以使用標(biāo)準(zhǔn)瀏覽器從webapps文件夾訪問其他網(wǎng)頁。現(xiàn)在嘗試訪問URL?http://localhost:8080/TestWeb/hello,如果您的Spring Web應(yīng)用程序一切正常,您應(yīng)該看到以下結(jié)果:
Maven示例:
https://github.com/easonjim/5_java_example/tree/master/springmvc/tutorialspoint/test1
?
轉(zhuǎn)載于:https://www.cnblogs.com/EasonJim/p/7500027.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Spring MVC-视图解析器(View Resolverr)-内部资源视图解析器(Internal Resource View Resolver)示例(转载实践)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 线性代数(六)正交性
- 下一篇: node进阶| 解决表单enctype=