當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringMVC快速入门-代码实现
生活随笔
收集整理的這篇文章主要介紹了
SpringMVC快速入门-代码实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
①導入Spring和SpringMVC的坐標、導入Servlet和Jsp的坐標
<!--Spring坐標--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.5.RELEASE</version></dependency><!--SpringMVC坐標--><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.0.5.RELEASE</version></dependency> <!--Servlet坐標--> <dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version> </dependency> <!--Jsp坐標--> <dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.0</version> </dependency>②在web.xml配置SpringMVC的核心控制器
<servlet><servlet-name>DispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name><url-pattern>/</url-pattern> </servlet-mapping>③創建Controller和業務方法
public class QuickController {public String quickMethod(){System.out.println("quickMethod running.....");return "index";} }③創建視圖頁面index.jsp
<html> <body><h2>Hello SpringMVC!</h2> </body> </html>④配置注解
@Controller public class QuickController {@RequestMapping("/quick")public String quickMethod(){System.out.println("quickMethod running.....");return "index";} }⑤創建spring-mvc.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc"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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--配置注解掃描--><context:component-scan base-package="com.leon"/> </beans>⑥訪問測試地址
http://localhost:8080/springmvc1/quick?
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的SpringMVC快速入门-代码实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringMVC快速入门-开发步骤
- 下一篇: SpringMVC流程图示