SpringMvc简单使用
SpringMvc框架的简单使用
- 第一步:导入依赖
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.1.RELEASE</version> </dependency> </dependencies>
同时,引入编译插件,使编译时使用JDK1.8版本(非必须)
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>
- 第二步:编辑
web.xml
文件
新增springmvc的入口servlet
<web-app> <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
contextConfigLocation
参数来指定springmvc.xml文件的位置
- 第三步:编辑
springmvc.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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.gosuncn"/> <mvc:annotation-driven/> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
- 第四步:编写
TestController
控制器
package com.gosuncn; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class TestController { @RequestMapping("/test") public String test() { return "success"; } }
- 最后一步:编写JSP文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Success</title> </head> <body> <h1>恭喜,成功了。</h1> </body> </html>
相关推荐
JudeJoo 2020-08-21
meleto 2020-08-15
whbing 2020-01-19
牧场SZShepherd 2020-01-12
boneix 2020-10-21
haidaoxianzi 2020-07-04
小鱿鱼 2020-06-26
咻pur慢 2020-06-15
haidaoxianzi 2020-06-14
qingjiuquan 2020-06-07
zhangdy0 2020-05-31
qingjiuquan 2020-05-30
HappyHeng 2020-05-16
whbing 2020-04-17
whbing 2020-04-11
小鱿鱼 2020-03-20
mendeliangyang 2020-02-20
方志朋 2020-02-14
牧场SZShepherd 2020-02-01