笔者接触JavaEE开发数年有余,从Struts1、Webwork、Struts2、SpringMVC3一路用过来,JavaEE的XML配置是最令人头疼的,随着工程逻辑复杂度的上升,XML文件变得越来越难以维护,这也是JavaEE最令人诟病的地方。不过问题总有解决的方法,新的Spring MVC3可以基于全注解驱动,代码编写极为方便,模块代码耦合度低,基本上可以做到“一次配置、随意编写”,是Web开发一大利器。Spring MVC3的改进最大,由于发布时间不长(其实也不短),网上文章不是很完善,特整理出一份相对简洁的配置给大家分享,不说了,直接上代码。
1、开发环境选择MyEclipse 10.6,建立新Web Project,选择JavaEE6.0(JavaEE5.0也可以)
2、拷贝相应的lib文件,standard.jar和jstl.jar是JSTL标签所需,freemarker-2.3.19.jar是freemarker所需,大家可以根据实际情况选择。
3、修改web.xml
<?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>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: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>spring-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: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>5、最后编写一个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,各种插件,在后续文章中会陆续介绍,待续。。。。。