欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Maven+SpringMVC简单框架搭建

程序员文章站 2022-06-10 17:46:41
...

1.创建Maven web 项目

 

A、

Maven+SpringMVC简单框架搭建
            
    
    博客分类: Mavenjava WebSpringMVC SpringMVCMaven配置 

 

  B、

 

 

此处根据项目情况自行配置: 
Maven+SpringMVC简单框架搭建
            
    
    博客分类: Mavenjava WebSpringMVC SpringMVCMaven配置 
 
 
 c、Maven+SpringMVC简单框架搭建
            
    
    博客分类: Mavenjava WebSpringMVC SpringMVCMaven配置  

 

2.完善Maven项目架构 

项目完善图:


Maven+SpringMVC简单框架搭建
            
    
    博客分类: Mavenjava WebSpringMVC SpringMVCMaven配置 

 

 

2.配置mavenpom.xml


Maven+SpringMVC简单框架搭建
            
    
    博客分类: Mavenjava WebSpringMVC SpringMVCMaven配置 
 

该文件中,在项目生成的时候就生成了部分项目信息的内容,我们不怎么需要修改的。只需添加Spring的依赖包。

<!--EndFragment-->

 

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.geronimo.specs</groupId>
        <artifactId>geronimo-servlet_2.5_spec</artifactId>
        <version>1.2</version>
    </dependency>
            
  </dependencies>

 

4.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
    <!-- Spring启动无法打开applicationContext.xml的时候,手动配置下 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:applicationContext.xml</param-value>
	</context-param>
	
	<!-- Spring 监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!--Spring 核心控制器 (必须项) -->
	<servlet>
		<servlet-name>exam</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath*:dispathcher-servlet.xml</param-value>
		</init-param>
	</servlet>

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

	<!-- 编码过滤器 -->
	<filter>
		<filter-name>CharacterEncodingFilter</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>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!--设置首页(必须项) -->
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

5.配置Spring applicationContext.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
</beans>

 

6.配置Spring dispatcher-servlet.xml

 

<!--EndFragment-->

 

<?xml version="1.0" encoding="UTF-8"?>
<!-- Bean头部 -->
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd              
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

	<!-- 激活@Controller模式 -->
	<mvc:annotation-driven />
	<!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 需要更改 -->
	<context:component-scan base-package="com.molv" />

	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/WEB-INF/jsp/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>
</beans>

 <!--EndFragment-->

<!--EndFragment--><!--EndFragment-->

  • Maven+SpringMVC简单框架搭建
            
    
    博客分类: Mavenjava WebSpringMVC SpringMVCMaven配置 
  • 大小: 82.6 KB
  • Maven+SpringMVC简单框架搭建
            
    
    博客分类: Mavenjava WebSpringMVC SpringMVCMaven配置 
  • 大小: 47.8 KB
  • Maven+SpringMVC简单框架搭建
            
    
    博客分类: Mavenjava WebSpringMVC SpringMVCMaven配置 
  • 大小: 84.5 KB
  • Maven+SpringMVC简单框架搭建
            
    
    博客分类: Mavenjava WebSpringMVC SpringMVCMaven配置 
  • 大小: 15.2 KB
  • Maven+SpringMVC简单框架搭建
            
    
    博客分类: Mavenjava WebSpringMVC SpringMVCMaven配置 
  • 大小: 40.8 KB