SpringMVC基本环境搭建(配置文件模板模板)

2022-12-21 08:51:41 浏览数 (1)

导包:

代码语言:javascript复制
<build> 
    <plugins> 
      <!-- 设置编译版本为1.8 -->  
      <plugin> 
        <groupId>org.apache.maven.plugins</groupId>  
        <artifactId>maven-compiler-plugin</artifactId>  
        <version>3.1</version>  
        <configuration> 
          <source>1.8</source>  
          <target>1.8</target>  
          <encoding>UTF-8</encoding> 
        </configuration> 
      </plugin> 
    </plugins>
<!--    Maven资源过滤设置-->
    <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>
      <!-- 导入依赖-->  
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.1.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

创建一个 spring-mvc.xml 文件

代码语言:javascript复制
<?xml version="1.0" encoding="UTF-8"?>
<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/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">
    
     <!--  包扫描-->
   <context:component-scan base-package="com.itcsdn.controller"/>
   
       <!--为了对springmvc的组件做增强-->
    <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>

web.xml 中配置:

代码语言:javascript复制
<!--MVC配置-->
	<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:spring-mvc.xml</param-value>
		</init-param>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>springMvc</servlet-name>
		<!--/* 代表所有的请求都会进入到前端控制器中,包含jsp
		/  代表所有的请求都会进入到前端控制器中,不包含jsp
		*.itcsdn   代表以 .itcsdn结尾的请求都会进入到前端控制器中-->
		<url-pattern>/</url-pattern>
	</servlet-mapping>

0 人点赞