Spring MVC 的核心应用-1
使用Spring MVC实现登录、注销
配置文件applicationcontext-jdbc.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xmlns:context="http://www.springframework.org/schema/context" 8 xsi:schemaLocation=" 9 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 10 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 11 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 12 http://www.springframework.org/schema/context 13 http://www.springframework.org/schema/context/spring-context.xsd"> 14 <!-- 15 使spring扫描包下的所有类,让标注spring注解的类生效 16 若扫描到有@Component @[email protected]等这些注解的类,则把这些类注册为bean 17 --> 18 <context:component-scan base-package="cn.smbms.service"/> 19 <context:component-scan base-package="cn.smbms.dao"/> 20 </beans>
配置web.xml
1 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> 2 <display-name>springMVC</display-name> 3 <welcome-file-list> 4 <welcome-file>/WEB-INF/jsp/login.jsp</welcome-file> 5 </welcome-file-list> 6 <context-param> 7 <param-name>contextConfigLocation</param-name> 8 <param-value>classpath:applicationContext-*.xml</param-value> 9 </context-param> 10 <filter> 11 <filter-name>encodingFilter</filter-name> 12 <filter-class> 13 org.springframework.web.filter.CharacterEncodingFilter 14 </filter-class> 15 <init-param> 16 <param-name>encoding</param-name> 17 <param-value>UTF-8</param-value> 18 </init-param> 19 <init-param> 20 <param-name>forceEncoding</param-name> 21 <param-value>true</param-value> 22 </init-param> 23 </filter> 24 <filter-mapping> 25 <filter-name>encodingFilter</filter-name> 26 <url-pattern>/*</url-pattern> 27 </filter-mapping> 28 <servlet> 29 <servlet-name>springmvc</servlet-name> 30 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 31 <init-param> 32 <param-name>contextConfigLocation</param-name> 33 <param-value>classpath:springmvc-servlet.xml</param-value> 34 </init-param> 35 <load-on-startup>1</load-on-startup> 36 </servlet> 37 <servlet-mapping> 38 <servlet-name>springmvc</servlet-name> 39 <url-pattern>/</url-pattern> 40 </servlet-mapping> 41 <context-param> 42 <param-name>log4jConfigLocation</param-name> 43 <param-value>classpath:log4j.properties</param-value> 44 </context-param> 45 <context-param> 46 <param-name>webAppRootKey</param-name> 47 <param-value>SMBMS_C09_04.root</param-value> 48 </context-param> 49 <listener> 50 <listener-class> 51 org.springframework.web.util.Log4jConfigListener 52 </listener-class> 53 </listener> 54 <listener> 55 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 56 </listener> 57 </web-app>
配置Springmvc-servlet.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context.xsd 12 http://www.springframework.org/schema/mvc 13 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 14 15 <context:component-scan base-package="cn.smbms.controller"/> 16 <mvc:annotation-driven/> 17 <mvc:resources mapping="/statics/**" location="/statics/" /> 18 <!-- 完成视图的对应 --> 19 <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 --> 20 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" > 21 <property name="prefix" value="/WEB-INF/jsp/"/> 22 <property name="suffix" value=".jsp"/> 23 </bean> 24 25 </beans>
静态资源文件的引用