Spring-MVC,带一小例子

以Spring 3.0作为例子。也需要导Spring 3.0 Web Libraries

一、在web.xml 配置里还需要加上如下代码

<!-- Spring MVC的Servlet,在WEB-INF下还要有一个<Servlet名>-servlet.xml的文件 -->	
<servlet>
	<display-name>Spring MVC的控制器</display-name>
	<servlet-name>dispatcher</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
	<servlet-name>dispatcher</servlet-name>
	<url-pattern>*.do</url-pattern>
</servlet-mapping>

这样,所有的.do的请求,都会被DispatcherServlet处理。初始化 DispatcherServlet 时,该框架在 web 应用程序WEB-INF 目录中寻找一个名为[servlet-名称]-servlet.xml的文件,并在那里定义相关的Beans,重写在全局中定义的任何Beans,像上面的web.xml中的代码,对应的是dispatcher-servlet.xml;当然也可以使用<init-param>元素,手动指定配置文件的路径。

符:汉字解码的过滤器,也是配置在web.xml中

<!-- 处理字符集 -->
<filter>
	<description>汉字解码</description>
	<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>

二、dispatcher-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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" 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-3.0.xsd  
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
            http://www.springframework.org/schema/context  
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
	<context:component-scan base-package="org.e276.action" />

	<!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

	<!-- 对模型视图名称的解释,即在模型视图名称添加前后缀 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/WEB-INF/page/" p:suffix=".jsp" />

</beans>

三、一些常用的注解

1.@Controller:注解标识一个控制器

2.@RequestMapping:注解标记一个访问的路径("index.do"),return "index"标记返回视图(index.jsp)。如果@RequestMapping注解在类级别上,则表示一相对路径,在方法级别上,则标记访问的路径。

3.@CookieValue:获取Cookie的值

四、国际化

在MVC配置文件中,配置国际化属性文件:

<bean id="messageSource"
	class="org.springframework.context.support.ResourceBundleMessageSource"
	p:basename="message">
</bean>

那么,Spring就会在项目中搜索相关的国际化属性文件,如:message.properties、message_zh_CN.properties。

在VIEW中,引入Spring标签:

<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>

使用<spring:message code="key" />调用,即可。

 

如果一种语言,有多个语言文件,可以更改MVC配置文件为:

<bean id="messageSource"
		class="org.springframework.context.support.ResourceBundleMessageSource">
	<property name="basenames">
		<list>
			<value>message01</value>
			<value>message02</value>
			<value>message03</value>
		</list>
	</property>
</bean>

 

 五、demo

 E276-Annotation.zip

猜你喜欢

转载自z18022893621.iteye.com/blog/1958585