筆者接觸JavaEE開發數年有餘,從Struts一、Webwork、Struts二、SpringMVC3一路用過來,JavaEE的XML配置是最使人頭疼的,隨着工程邏輯複雜度的上升,XML文件變得愈來愈難以維護,這也是JavaEE最使人詬病的地方。不過問題總有解決的方法,新的Spring MVC3能夠基於全註解驅動,代碼編寫極爲方便,模塊代碼耦合度低,基本上能夠作到「一次配置、隨意編寫」,是Web開發一大利器。Spring MVC3的改進最大,因爲發佈時間不長(其實也不短),網上文章不是很完善,特整理出一份相對簡潔的配置給你們分享,不說了,直接上代碼。 html
一、開發環境選擇MyEclipse 10.6,創建新Web Project,選擇JavaEE6.0(JavaEE5.0也能夠) java
二、拷貝相應的lib文件,standard.jar和jstl.jar是JSTL標籤所需,freemarker-2.3.19.jar是freemarker所需,你們能夠根據實際狀況選擇。 web
三、修改web.xml spring
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>encoding</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>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
api
applicationContext.xml spring-mvc
----------------------------------------------------------------- 服務器
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
</beans> mvc
spring-servlet.xml app
--------------------------------------------------------------------------- 異步
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="controller"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- 如下配置將採用JSTL標籤庫-->
<!--
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
-->
<!-- 如下配置將採用freemarker標籤庫-->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath">
<value>/WEB-INF/</value>
</property>
<property name="freemarkerVariables">
<map>
<entry key="xml_escape" value-ref="fmXmlEscape" />
</map>
</property>
<property name="defaultEncoding">
<value>utf-8</value>
</property>
<property name="freemarkerSettings">
<props>
<prop key="whitespace_stripping">true</prop>
<prop key="default_encoding">UTF-8</prop>
<prop key="tag_syntax">auto_detect</prop>
<prop key="url_escaping_charset">UTF-8</prop>
<prop key="number_format">0.######</prop>
</props>
</property>
</bean>
<bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
<property name="suffix" value=".ftl" />
<property name="order" value="1" />
<property name="contentType" value="text/html;charset=utf-8" />
<property name="cache" value="false" />
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="i18n/messages"/>
</bean>
<!-- 處理Mutipart文件上傳 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
</bean>
</beans>
五、最後編寫一個controller,helloworld,這裏採用freemarker標籤
PortalController.java
-------------------------------------------------------------------
package controller;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller("portalController")
@SuppressWarnings("unchecked")
public class PortalController {
@RequestMapping(value="/index",method=RequestMethod.GET)
public String index(Map model,HttpServletRequest request,HttpServletResponse response){
String message="hello world";
model.put("message", message);
return "/index"; //此處的路徑和配置文件中的先後綴拼起來就是模板文件的完整路徑,此處完整路徑即/WEB-INF/index.ftl
}
}
在WEB-INF目錄下添加index.ftl文件
index.ftl
設置J2EE服務器,筆者使用Glassfish3.1.2,將工程設爲默認工程,這樣訪問時不須要加工程名字,在MyEclipse中Glassfish啓動後訪問4848端口的控制檯,進行設置,用Tomcat的兄弟姐妹們請本身搞定^_^
設置完畢後,測試訪問
Spring 3.0幾乎實現了全註解驅動開發,從MVC到數據訪問,定時器,Http異步I/O,各類插件,在後續文章中會陸續介紹,待續。。。。。