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

springmvc环境搭建之配置和所需jar包 博客分类: java javaspringspringmvc 

程序员文章站 2024-03-16 16:31:22
...

1.web.xml 配置

  <!-- 加载spring配置文件 -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/Spring.xml</param-value><!-- ,/WEB-INF/Spring-Mybatis.xml -->

</context-param>

 

<servlet>

<servlet-name>dispatcherServlet</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<!-- SpringMVC配置文件 -->

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/SpringMVC.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>dispatcherServlet</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

 

<filter>

<description>字符集过滤器</description>

<filter-name>encodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<description>字符集编码</description>

<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>

 

<!-- spring的监听器 -->

<listener>

<description>spring监听器</description>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

 

<!-- 防止spring内存溢出监听器 -->

<listener>

<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>

</listener>

 

2.Spring.xml 配置

 

<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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"

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

">

<mvc:annotation-driven/>

<!--引入属性文件  -->

<!--<context:property-placeholder location="classpath:jdbc.properties" />-->

<!-- service和dao包(自动注入) -->

<context:component-scan base-package="com.service,com.service.impl,com.po" /> 

 

</beans>

 

3.SpringMVC.xml 配置

 

<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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"

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

">

<mvc:annotation-driven />  <!-- 支持spring3.0新的mvc注解 -->

<context:component-scan base-package="com.controller" />

 

<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->

<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">

<property name="supportedMediaTypes">

<list>

<value>text/html;charset=UTF-8</value>

</list>

</property>

</bean>

 

<!-- 启动Spring MVC 的注解功能,完成请求和注解POJO的映射 -->

<bean

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

<property name="messageConverters">

<list>

<ref bean="mappingJacksonHttpMessageConverter" />  <!-- json转换器 -->

</list>

</property>

</bean>

 

<!-- 页面View层基本信息设定 -->

<bean id="viewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="viewClass"value="org.springframework.web.servlet.view.JstlView" />

</bean>

 

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<property name="defaultEncoding" value="UTF-8" />

<property name="maxUploadSize" value="32505856" /><!-- 上传文件大小限制为31M,31*1024*1024 -->

<property name="maxInMemorySize" value="4096" />

</bean>

 

</beans>

 

以上就是简单的配置,附件是所需的jar包。