[spring mvc]Hello World入门
1.新建項(xiàng)目
File->New->Other,選擇Dynamic web project:
?
?
項(xiàng)目建好之后,目錄結(jié)構(gòu)如下:
?
?
2.WEB-INF/web.xml 中配置 dispatcherServlet
?
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1"><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><servlet><servlet-name>spring-dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 配置Spring mvc下的配置文件 ,如果不配置,默認(rèn)加載WEB-INF下XXX-servlet.xml(XXX為上面的<servlet-name>)--><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/spring-*.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet> ... </web-app>
?
3.在src目錄下新建spring-web.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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"><!-- 配置自動(dòng)掃描的包 --><context:component-scan base-package="com.jackie.springmvc"></context:component-scan><!-- 配置視圖解析器 如何把handler 方法返回值解析為實(shí)際的物理視圖 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name = "prefix" value="/WEB-INF/views/"></property><property name = "suffix" value = ".jsp"></property></bean></beans>?
?
4.HelloWorld.java(com.jackie.springmvc.handlers下)
?
//1. 首先要在類(lèi)的前面添加“Controller”注解,表示是spring的控制器@Controllerpublic class HelloWorld {
//@RequestMapping, 是用于匹配請(qǐng)求的路徑,比如這里匹配的請(qǐng)求路徑就是“http://localhost:8080/springTest/springmvc/helloworld” @RequestMapping("/helloworld")public String hello(){System.out.println("hello world");
//這個(gè)返回的字符串與上面springmvc.xml進(jìn)行配合,springmvc.xml中聲明了prefix和suffix,而夾在這兩者之間的就是這里返回的字符串,所以執(zhí)行完這個(gè)方法后,我們可以得到這樣的請(qǐng)求資源路徑“/WEB-INF/views/success.jsp”,這個(gè)success.jsp是需要我們新建的return "success";}}
?
5.index.jsp(WebContent下)
在新建success.jsp之前,我們需要有一個(gè)入口,也就是這里的index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body><a href="helloworld">hello world</a></body></html>當(dāng)訪問(wèn)index.jsp時(shí),頁(yè)面上會(huì)展示一個(gè)超鏈接,點(diǎn)擊超鏈后,url中的地址就會(huì)發(fā)生跳轉(zhuǎn),由“http://localhost:8080/springTest/index.jsp”跳轉(zhuǎn)到“http://localhost:8080/springTest/helloworld”,而這個(gè)url請(qǐng)求就會(huì)進(jìn)入HelloWorld中的hello方法,因?yàn)槠渑c該方法上的“/helloworld”匹配
6.success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>success</title> </head> <body> <h4>Success Page</h4> </body> </html>?
轉(zhuǎn)載于:https://www.cnblogs.com/vickylinj/p/9518929.html
總結(jié)
以上是生活随笔為你收集整理的[spring mvc]Hello World入门的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SharpZipLib压缩解压
- 下一篇: 【转载】什么是C++虚函数、虚函数的作用