SpringMVC 在 MAVEN中的配置步骤

1.首先需要建立一个maven的webapp工程


image.png

点击Next后


image.png

再点击Next
image.png

注意 Packaging中的选项选为war
然后在项目的properties中找到Project Facets选择适合的版本


image.png

然后找到Java Build Path 点击 Add Library..


image.png

点击Server Runtime然后Next再加入Tomcat


image.png

2.然后打开pom.xml文件导入所需的包

  <dependency>  
  <groupId>junit</groupId>  
  <artifactId>junit</artifactId>  
  <version>4.10</version>  
  <scope>test</scope>  
</dependency>  
 <dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-webmvc</artifactId>  
    <version>3.2.8.RELEASE</version>  
</dependency>  
<dependency>  
    <groupId>jstl</groupId>  
    <artifactId>jstl</artifactId>  
    <version>1.1.2</version>  
</dependency>  

3.导完包后我们需要进行配置spring-mvc.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:tx="http://www.springframework.org/schema/tx"  
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-3.2.xsd   
http://www.springframework.org/schema/tx   
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-3.2.xsd  
http://www.springframework.org/schema/mvc  
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">  

<!-- 自动扫描的包名 -->  
<context:component-scan base-package="com.test"/>  

<!-- 默认的注解映射的支持,自动注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->  
<mvc:annotation-driven />  

<!-- 视图解释类 -->  
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    <property name="prefix" value="/WEB-INF/jsp/"/>  
    <property name="suffix" value=".jsp"/>  
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />  
</bean>  
</beans>   

4.再配置web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns="http://java.sun.com/xml/ns/javaee"   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>test</display-name>

<!-- 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:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

至此配置过程就完成了,接下来就可以写springMVC项目啦

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容