搭建spring+springmvc环境
学了不用就忘了,啊啊啊啊啊啊,学的不如忘得快
maven环境搭建
在idea中新建一个maven项目,这里都会不在赘述了。搭建目录文件选中项目名按F12
在这里添加缺少的文件
标准的项目目录就是
注意 idea中一定要把配置文件放到resources中,否则识别不到
搭建spring环境
在web.xml中添加spring配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
在resources下新建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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 开启注解扫描,要扫描的是service和dao层的注解,要忽略web层注解,因为web层让SpringMVC框架 去管理 -->
<context:component-scan base-package="com.ysl">
<!-- 配置要忽略的注解 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
spring搭建完成!
搭建springmvc环境
在web.xml中添加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:springmvc.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>
在resources下新建springmvc.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:mvc="http://www.springframework.org/schema/mvc"
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">
<!--开启注解扫描,扫描controller的注解,别的不扫描-->
<context:component-scan base-package="com.learn">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:default-servlet-handler></mvc:default-servlet-handler>
</beans>
丰富版——比较全的
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:mcv="http://www.springframework.org/schema/mvc"
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">
<!--开启注解扫描,扫描controller的注解,别的不扫描-->
<context:component-scan base-package="com.ysl">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置自定义类型转换器-->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.ysl.utils.StringToDateConverter"/>
</set>
</property>
</bean>
<!--配置文件解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760"/>
</bean>
<!--前端控制器,哪些静态资源不拦截-->
<mcv:resources location="/js/" mapping="/js/**"/>
<mcv:resources location="/css/" mapping="/css/**"/>
<mcv:resources location="/images/" mapping="/images/**"/>
<!--配置异常处理器-->
<bean id="sysExceptionResolver" class="com.ysl.exception.SysExceptionResolver"/>
<!--配置拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<!--要拦截的具体的方法 /**表示全都拦截 /user/*表示只拦user开头的-->
<mvc:mapping path="/interceptor/*"/>
<!--不拦截的方法
<mvc:exclude-mapping path="">
-->
<!--配置拦截器对象-->
<bean class="com.ysl.interceptor.MyInterceptor1"/>
</mvc:interceptor>
<mvc:interceptor>
<!--要拦截的具体的方法 /**表示全都拦截 /user/*表示只拦user开头的-->
<mvc:mapping path="/**"/>
<!--不拦截的方法
<mvc:exclude-mapping path="">
-->
<!--配置拦截器对象-->
<bean class="com.ysl.interceptor.MyInterceptor2"/>
</mvc:interceptor>
</mvc:interceptors>
<!--开启SpringMVC框架注解的支持-->
<mvc:annotation-driven conversion-service="conversionService"/>
</beans>
就只写配置文件,至于拦截器、异常处理类怎么写后面再补充。这里只介绍环境的搭建。
springmvc搭建完成!
配置tomcat
启动一下就可以访问了!
这样就说明成功了
注意上面那个application context如果不为空的话要在访问路径上添上