spring-mvc入门

spring-mvc入门

一、web.xml配置:

<span style="font-size:18px;"><?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: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_2_5.xsd"
	id="WebApp_ID" version="2.5">
	
	<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>
	
	<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:spring-mvc.xml</param-value>
		</init-param>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
</web-app></span>

二、applicationContext.xml:

<span style="font-size:18px;"><?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	
	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">
	
	
</beans></span>

三、spring-mvc.xml(最重要的配置文件):
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	
	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">
		
	<context:component-scan base-package="com.plateno.web.action" />
	
	<bean name="/index.html" class="com.plateno.web.action.HomeAction"></bean><!-- HomeAction要实现Controller接口 -->
	<bean name="/" class="com.plateno.web.action.HomeAction"></bean>
	<!-- 我知道的四种HandlerMapping -->
	<!-- BeanNameUrlHandlerMapping和SimpleUrlHandlerMapping对应的handlerAdaptor都是SimpleControllerHandlerAdapter 他们都处理url和action的对应关系-->
	<bean id="simpleControllerHandlerAdapter" class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
	<bean id="beanNameUrlHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
		<property name="interceptors" ref="httpRequestInterceptor"></property>
		<property name="order" value="2"></property>
	</bean>
	
	<bean id="simpleUrlAction" class="com.plateno.web.action.SimpleUrlAction"></bean> <!-- SimpleUrlAction要实现Controller接口 -->
	<bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="interceptors" ref="httpRequestInterceptor"></property>
		<property name="order" value="1"></property>
		<property name="urlMap">
			<map>
				<entry key="/" value-ref="simpleUrlAction"/>
				<entry key="/index.html" value-ref="simpleUrlAction"/>
			</map>
		</property>
	</bean>
	<!-- 每种HandlerMapping都有对应的HandlerHandler,如果不对应,会报错 -->
	<!-- DefaultAnnotationHandlerMapping和RequestMappingHandlerMapping都是处理requestMapping的注解,前者以废弃,不建议使用,后者用来代替前者 -->
	<bean id="annotationMethodHandlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
	<bean id="defaultAnnotationHandlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
		<property name="interceptors">
			<list>
				<ref bean="httpRequestInterceptor"/>
			</list>
		</property>
		<property name="order" value="3"></property>
	</bean>
	
	<bean id="requestMappingHandlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
	<bean id="requestMappingHandlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
		<property name="interceptors">
			<list>
				<ref bean="httpRequestInterceptor"/>
			</list>
		</property>
		<property name="order" value="4"></property>
	</bean>
	
	<bean id="freeMarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="templateLoaderPath" value="/WEB-INF/pages/"/>
	</bean>
	
	<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		<property name="prefix" value=""/>
		<property name="suffix" value=".html"/>
		<property name="contentType" value="text/html;charset=UTF-8"/>
	</bean>	 
	<!-- handler处理之前,可以有拦截器,继承HandlerInterceptor接口就ok -->
	<bean id="httpRequestInterceptor" class="com.plateno.interceptor.HttpRequestInterceptor"></bean>
</beans></span>


里面都带了注释,java代码太简单,在这里就不贴。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值