配置spring和sprinMVC框架
来源:互联网 发布:网络公司名称大全 编辑:程序博客网 时间:2024/06/06 01:42
参考该文章:http://www.cnblogs.com/superjt/p/3309255.html
1、网上下载spring相关的jar
网上找到的spring框架的jar的库
url:http://repo.spring.io/release/org/springframework/spring
还需要commons-logging-1.2.jar包
如果使用tomcat还要jstl-1.2.jar包
2、配置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" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="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_2_5.xsd" id="monitorweb" version="2.5"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- Spring MVC配置 --> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- Spring配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:application-context.xml, classpath:application-redis.xml </param-value> </context-param> <!-- 增加编码过滤器 --> <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><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param> </filter> <filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern> </filter-mapping> <!-- jsp配置 --> <jsp-config> <jsp-property-group> <display-name>lib</display-name> <url-pattern>*.jsp</url-pattern> <page-encoding>UTF-8</page-encoding> </jsp-property-group> </jsp-config></web-app>
3、配置servlet.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 开启注解处理器 --><context:annotation-config/><!-- 开启组件自动扫,扫描路径由base-package属性指定,貌似只能有一个组件扫描component-scan --><context:component-scan base-package="core.controller" use-default-filters="false"> <!-- 扫描type类型为注解的@Controller的文件 --><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!-- 开启aop代理,要在SpringMVC配置中开启 --><aop:aspectj-autoproxy proxy-target-class="true"/><!-- 完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- <mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean, 配置一些messageconverter。即解决了@Controller注解的使用前提配置。 --> <mvc:annotation-driven conversion-service="conversionService"> <!-- 注解方法处理适配器 --> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/> <bean class="org.springframework.http.converter.FormHttpMessageConverter" /><bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /><bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" /><bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter" /><bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"><property name="objectMapper" ref="jacksonObjectMapper"/><property name="supportedMediaTypes"><list><value>application/json;charset=UTF-8</value><value>text/plain;charset=UTF-8</value></list></property></bean></mvc:message-converters></mvc:annotation-driven><beanclass="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"><property name="caseSensitive" value="true"></property></bean><bean id="conversionService"class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><property name="registerDefaultFormatters" value="false" /><property name="formatterRegistrars"><set><bean class="org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar"><property name="dateFormatter"><bean class="org.springframework.format.datetime.joda.DateTimeFormatterFactoryBean"><property name="pattern" value="yyyy-MM-dd HH:mm:ss" /></bean></property></bean></set></property></bean><bean name="jacksonObjectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"><property name="dateFormat"><bean class="java.text.SimpleDateFormat"><constructor-arg type="java.lang.String" value="yyyy-MM-dd" /></bean></property><!--<property name="featuresToDisable"><array><util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS"/></array></property>--></bean> <!-- 处理映射 --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> <!-- 处理适配器 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> <!-- 表示层解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean></beans>
4、配置applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"><!-- 开启注解处理器 --><context:annotation-config/><!-- 外在化应用参数的配置 --><context:property-placeholder location="classpath:application-config.properties"ignore-unresolvable="true" /><!-- 开启组件自动扫,扫描路径由base-package属性指定,貌似只能有一个组件扫描component-scan --><context:component-scan base-package="core.aspect,service,utils"> <!-- 过滤掉type类型为注解的表达式为@Controller的文件 --><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan></beans>
package test.controller;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;import test.Test;@Controller@RequestMapping("/login")public class LoginController {@Resourceprivate Test test;@RequestMapping(value = "index", method = RequestMethod.GET)public ModelAndView index(){ModelAndView mav = new ModelAndView("a");return mav;}@RequestMapping(value = "add", method = RequestMethod.GET)public Object add(int a){test.testSave(); return null;}}
注意:依赖注入的类必须先声明bean,可在配置文件声明bean或在类文件打注解(@Component、@Service等等)
0 0
- 配置spring和sprinMVC框架
- Spring+SprinMVC配置学习总结
- ssm (spring,sprinMvc,mybatis)集成
- Spring框架简介和初次框架配置
- spring管理事务失效的原因和No mapping found for HTTP request with URI []DispatcherServlet with name 'sprinmvc错误
- spring mvc框架搭建和配置
- 使用sprinmvc+spring+mybatis整合的时ASM报错
- Spring框架学习【Spring读取和处理事务配置】
- spring框架如何配置
- Spring框架的配置
- Spring框架的配置
- spring 框架配置类
- Spring框架介绍和IoC容器中Bean配置(Spring框架)
- Spring框架介绍和IoC容器中Bean配置(Spring框架)
- 利用SPRING JDBC 和模板框架实现SQL可配置
- Spring框架学习【解析和注入注解配置的资源】
- Spring框架学习【数据源和数据库连接池配置】
- Spring框架中的定时器 如何使用和配置
- Android 蓝牙自动打开并扫描设备,以及获取对方蓝牙设备的种类
- 微信小程序教程(第四篇)
- Linux中普通进程、后台进程和守护进程
- 微信js分享
- gtest 中Setup TearDown SetUpTestCase和TearDownTestCase 的区别
- 配置spring和sprinMVC框架
- git push 小结
- 为苹果ATS和微信小程序搭建 Nginx + HTTPS 服务
- Android DataBinding介绍(一)——简介、数据及方法事件绑定
- 必须掌握的C++常用关键字汇总
- MySQL Group Replication调研剖析
- SpringMVC解决GET请求时中文乱码的问题
- tomcat配置文件server.xml详解
- 微信企业号开发—发送消息(文本、图片、语音)