創建第一個springmvc程式 1、創建父項目文件,導入依賴,刪除src文件夾 pom.xml文件 <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</ ...
創建第一個springmvc程式
1、創建父項目文件,導入依賴,刪除src文件夾
pom.xml文件
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl-api -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
</dependencies>
2、創建子項目文件,導入jar包
pom.xml
<dependencies>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
3、右鍵子項目文件,添加框架支持,選中web框架
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--寫死的-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--根據classpath創建spring對應的applicationContext.xml文件-->
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--啟動級別:1為伺服器啟動就開啟,值越接近1優先順序越高-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--在springmvc中,
/ :會匹配所有請求,除了jsp
/* :會匹配所有請求,包括jsp,會導致無限迴圈的hello.jsp.jsp.jsp.....
-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4、創建applicationContext.xml文件,添加對應bean
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<!--添加spring的BeanNameUrlHandlerMapping、SimpleControllerHandlerAdapter和InternalResourceViewResolver-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!--根據prefix找到文件夾目錄,根據suffix找到對應類型的文件-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--將寫好的servlet類加入bean中,id為請求的路徑-->
<bean id="/hello" class="com.kuang.controller.HelloController"/>
</beans>
5、編寫bean中對應的servlet類
package com.kuang.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//實現controller介面,重寫handleRequest方法,返回一個視圖
public class HelloController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
//新建一個modelandview
ModelAndView mv = new ModelAndView();
//添加對象
mv.addObject("msg","HelloSpringMVC");
//設置返回的視圖,不需要尾碼名,在applicationcontext中添加過suffix
mv.setViewName("hello");
//將對應視圖返回
return mv;
}
}
6、可能遇到的問題
404找不到文件
不是你代碼的問題,可以查看依賴是否成功導入
7、通過tomcat啟動運行,在瀏覽器中訪問http://localhost:8080/hello
程式運行成功