Spring MVC_Hello World
2021-06-19 11:05
YPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
æ ç¾ï¼lan   page   coding   web app   ring   encoding   ref   source   ati  Â
ã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 8 9 servlet> 10 servlet-name>springDispatcherServletservlet-name> 11 servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> 12 13 init-param> 14 param-name>contextConfigLocationparam-name> 15 param-value>classpath:springmvc.xmlparam-value> 16 init-param> 17 load-on-startup>1load-on-startup> 18 servlet> 19 20 servlet-mapping> 21 servlet-name>springDispatcherServletservlet-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 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 2 pageEncoding="ISO-8859-1"%> 3 4 5 6 7Insert title here 8 9 10 Hello World 11 12
Â
success.jspï¼
1 2 pageEncoding="ISO-8859-1"%> 3 4 5 6 7Insert title here 8 9 10SUCCESS
11 12
Â
è¿è¡ç»æï¼
Â
ç¹å»âHello Worldâ
Â
éï¼
Â
Spring MVC_Hello World
æ ç¾ï¼lan   page   coding   web app   ring   encoding   ref   source   ati  Â
åæå°åï¼https://www.cnblogs.com/zhzcode/p/9690388.html