日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring MVC_Hello World

發布時間:2025/3/15 javascript 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring MVC_Hello World 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【Hello World】

步驟:

(1)加入jar包,

(2)在web.xml中配置DispatcherServlet,

(3)加入Spring MVC的配置文件,

(4)編寫處理請求的處理器,并標識為處理器

(5)編寫視圖

?

(1)

?

(2)

web.xml:

1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 5 id="WebApp_ID" version="2.5"> 6 7 <!-- 配置DispatcherServlet --> 8 <!-- The front controller of this Spring Web application, responsible for handling all application requests --> 9 <servlet> 10 <servlet-name>springDispatcherServlet</servlet-name> 11 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 12 <!-- 配置DispatcherServlet的一個初始化參數:配置Spring MVC配置文件的位置和名稱 --> 13 <init-param> 14 <param-name>contextConfigLocation</param-name> 15 <param-value>classpath:springmvc.xml</param-value> 16 </init-param> 17 <load-on-startup>1</load-on-startup> 18 </servlet> 19 20 <servlet-mapping> 21 <servlet-name>springDispatcherServlet</servlet-name> 22 <url-pattern>/</url-pattern> 23 </servlet-mapping> 24 </web-app>

?

(3)

springmvc.xml:

1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 9 10 <!-- 配置自動掃描的包 --> 11 <context:component-scan base-package="com.hk.springmvc"></context:component-scan> 12 13 <!-- 配置視圖解析器:如何把handler方法返回值解析為物理視圖 --> 14 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 15 <property name="prefix" value="/WEB-INF/views/"></property> 16 <property name="suffix" value=".jsp"></property> 17 </bean> 18 </beans>

?

(4)

1 package com.hk.springmvc.handlers; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 //控制器 7 @Controller 8 public class HelloWorld { 9 10 /* 11 * 1.使用@RequestMapping注解來映射請求的URL 12 * 2.返回值會通過視圖解析器解析為物理的視圖,對于InternalResourceViewResolver 視圖解析器會做如下的解析: 13 * prefix + returnval + 后綴, 14 * 通過這樣的方式得到實際的物理視圖,然后做轉發操作 15 * /WEB-INF/views/success.jsp 16 */ 17 @RequestMapping("/helloworld") 18 public String hello(){ 19 System.out.println("Hello World"); 20 return "success"; 21 } 22 23 }

?

(5)

index.jsp:

1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 pageEncoding="ISO-8859-1"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <a href="helloworld">Hello World</a> 11 </body> 12 </html>

?

success.jsp:

1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 pageEncoding="ISO-8859-1"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <h4>SUCCESS</h4> 11 </body> 12 </html>

?

運行結果:

?

點擊“Hello World”

?

附:

?

轉載于:https://www.cnblogs.com/zhzcode/p/9690388.html

總結

以上是生活随笔為你收集整理的Spring MVC_Hello World的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。