第一個springmvc專案HelloSpringMVC
一、配置方式
1、建立一個父專案,新建一個Module,如springmvc-03,完成後選擇已建立的module右擊選Add Framwork Support新增web支援
2、確定匯入了SpringMVC依賴(在pom.xml檔案中新增springmvc和servlet依賴)
說明:若父專案中添加了這些依賴,子專案繼承父專案可以不用在自己的pom檔案中再次新增
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.12.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> </dependencies>
3、配置web.xml,註冊DispatcherServlet
<?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"> <!-- 配置DispatcherServlet:這個是springmvc的核心;請求分發器,前端控制器--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!-- 啟動級別: 跟伺服器一起啟動 --> <load-on-startup>1</load-on-startup> </servlet> <!-- springmvc中/ 與/*是不同的 /:匹配除jsp之外的所有請求 /*:包括jsp在內的所有請求--> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
4.在resoueces編寫SpringMVC 的 配置檔案!如名稱:springmvc-servlet.xml 需同web.xml中配置的中classpath值相同
<?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"> <!--配置處理器對映器--> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!--配置處理器介面卡--> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!--配置檢視解析器:DispatcherServlet給他的ModelAndView--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver"> <!--字首--> <property name="prefix" value="/WEB-INF/jsp/"/> <!--字尾--> <property name="suffix" value=".jsp"/> </bean> <bean id="/hello" class="com.zhou.controller.HelloController"/> </beans>
5.寫Contoroller,使用ModelAndView封裝資料和檢視物件
public class HelloController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView();
//業務程式碼
String result="HelloSpringMVC";
mv.addObject("msg",result);
//檢視跳轉
mv.setViewName("test");
return mv;
}
}
6.將Controller交給SpringIOC容器管理(在springmvc-servlet.xml中註冊Bean)
<bean id="/hello" class="com.zhou.controller.HelloController"/>
7、編寫前端檢視頁面
<%--
Created by IntelliJ IDEA.
User: mm
Date: 2022/5/7
Time: 10:58
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
8、釋出專案,啟動Tomcat後,在瀏覽器輸入localhost:8080/hello訪問頁面
二、註解方式(推薦)
1、建立一個新的空的maven專案(父專案),新建一個module,如springmvc-02-annotation
2、新增web支援(同方案一),右擊新建module,點選 Add Framwork Support新增web支援
3、在pom.xml中匯入springmvc和servlet依賴
<!--1.註冊DispatcherServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--關聯一個springmvc的配置檔案:【servlet-name】-servlet.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--啟動級別-1-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--/ 匹配所有的請求;(不包括.jsp)-->
<!--/* 匹配所有的請求;(包括.jsp)-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
4、在resoueces編寫SpringMVC 的 配置檔案!如名稱:springmvc-servlet.xml 需同web.xml中配置的中classpath值相同
<?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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 自動掃描包,讓指定包下的註解生效 ,由IOC容器統一管理-->
<context:component-scan base-package="com.zhou.controller"/>
<!-- 讓springmvc不處理靜態資源-->
<mvc:default-servlet-handler/>
<!-- 支援MVC註解驅動-->
<mvc:annotation-driven/>
<!--配置檢視解析器:DispatcherServlet給他的ModelAndView-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--字首-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--字尾-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
5、寫controller,使用ModelAndView封裝資料模型和物件
<?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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 自動掃描包,讓指定包下的註解生效 ,由IOC容器統一管理-->
<context:component-scan base-package="com.zhou.controller"/>
<!-- 讓springmvc不處理靜態資源-->
<mvc:default-servlet-handler/>
<!-- 支援MVC註解驅動-->
<mvc:annotation-driven/>
<!--配置檢視解析器:DispatcherServlet給他的ModelAndView-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--字首-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--字尾-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
6、編寫前端檢視頁面
<%--
Created by IntelliJ IDEA.
User: 周志飛
Date: 2022/5/7
Time: 16:57
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
7、釋出專案,啟動Tomcat後,在瀏覽器輸入localhost:8080/hello訪問頁面