您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“SpringMVC工程如何搭建”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“SpringMVC工程如何搭建”吧!
1、新建一個(gè)項(xiàng)目名為:springmvc-demo-yuyongqing
右鍵項(xiàng)目名選擇Add Framework Support
2、選擇Web Application
3、配置SpringMVC
pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
刷新maven后再加入如下所示代碼
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
1、
<?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
">
<!-- bean definitions here -->
2、添加SpringMVC配置內(nèi)容
<!-- 自動(dòng)掃描包,讓指定包下的注解生效,由IOC容器統(tǒng)一管理 -->
<context:component-scan base-package="controller"/>
<!-- 1加載注解驅(qū)動(dòng) -->
<mvc:annotation-driven/>
<!-- 2靜態(tài)資源過濾 -->
<mvc:default-servlet-handler/>
<!-- 3視圖解析器 -->
<bean id="internalResourceViewResolver" class=
"org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
3、Controller層
新建一個(gè)HelloController類
package controller;
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(Model model){
// Model 封裝數(shù)據(jù)
model.addAttribute("msg","HELLO MY FIRST SPRING MVC PROJECT");
// 返回的字符串就是視圖的名字 會(huì)被視圖解析器處理
return "hello";
}
}
4、JSP
在JSP包下新建hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
1、配置前端控制器
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
2、配置初始化參數(shù)
<!-- 配置初始化參數(shù) -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationcontext.xml</param-value>
</init-param>
3、設(shè)置啟動(dòng)級(jí)別
<!-- 設(shè)置啟動(dòng)級(jí)別 -->
<load-on-startup>1</load-on-startup>
</servlet>
4、設(shè)置SpringMVC攔截請(qǐng)求
<!-- 設(shè)置SpringMVC攔截請(qǐng)求 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern> / </url-pattern> <!--攔截除.jsp的請(qǐng)求-->
</servlet-mapping>
5、亂碼過濾
<!-- 亂碼過濾 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
6、運(yùn)行web
打包
File→Project Structure
刪除默認(rèn)的包
點(diǎn)ok→ok
1、點(diǎn)擊 Add Configuration… 進(jìn)入運(yùn)行配置框
2、點(diǎn) + 選擇Tomcat Server 下的 Local
3、點(diǎn)擊 Configure 選擇我們自己的TomCat
在瀏覽器輸入http://localhost:8080/hello
到此,相信大家對(duì)“SpringMVC工程如何搭建”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。