springMVC4.0+tiles2整合笔记

来源:互联网 发布:python vim 配置 编辑:程序博客网 时间:2024/05/22 16:43

看出来了,整合不同的框架就不停的配置,导包,缺一个就够你折腾半天的,没办法,谁叫人家每次升级都会与旧版本有不同的配置呢。时间都浪费在配置上了。估计下次用tiles3又会有所不同了。(首先demo项目来自spring in action 第三版第七章练习)

1.首先是web.xml,这里只要配置好springMVC就可以了,像一些教程还配置了一堆tiles的,现在最简

<?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"><display-name>springmvc</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file></welcome-file-list><!--spring mvc初始化配置文件,自动加载spitter-servlet.xml--><servlet>  <servlet-name>spitter</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping><servlet-name>spitter</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>


2.配置spitter-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:p="http://www.springframework.org/schema/p"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.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><!--默认注解映射--><mvc:annotation-driven/><!--对静态资源文件的访问,以/resources开头的请求,由/resources/底下的文件提供服务--><mvc:resources mapping="/resources/**" location="/resources" cache-period="31556926"/><mvc:default-servlet-handler/><!--spring 组件扫描配置 --><context:component-scan base-package="com.sam.controller"/><context:component-scan base-package="com.sam.service"/>   <!-- ViewResolver --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/><!--jsp使用了jstl需要将原来默认的InternalResourceView替换为JstlView--><property name="prefix" value="/WEB-INF/jsp/"/><property name="suffix" value=".jsp"/></bean><!-- 文件解析器--><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:maxUploadSize="500000"></bean><!--集成Apache tiles视图--><bean id="tilesviewResolver"class="org.springframework.web.servlet.view.tiles2.TilesViewResolver"><property name="order" value="0"></property></bean><bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"><property name="definitions"><list><value>/WEB-INF/jsp/**/views.xml</value><!--定义tiles布局--></list></property><property name="preparerFactoryClass"value="org.springframework.web.servlet.view.tiles2.SpringBeanPreparerFactory"/></bean></beans>

 

3.配置文件只要这样就行了,关键是jar的导入才让人烦哪,建议分开导入

springMVC相关的jar包

jstl.jar跟standard.jar因为jsp页面jstl标签需要

tiles相关的jar包,(可以到Apache官网下载)这里是2.0的,3.0的又有点不一样

框中选中的从官网下载的2.0版本lib里没有,会报错,所以从3.0的里面拷贝过来,注意上图中的commons也是必须的,缺少哪个就到Apache去下载都有

现在就可以使用tiles了。

我们在spitter-servlet.xml里面设置了tiles配置文件的位置为/WEB-INF/jsp/**/views.xml,即jsp文件夹下所有views.xml文件

views.xml

<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN""http://tiles.apache.org/dtds/tiles-config_2_1.dtd"><tiles-definitions><definition name="template" template="/WEB-INF/jsp/main_template.jsp"><put-attribute name="top" value="/WEB-INF/jsp/tiles/spittleForm.jsp"></put-attribute><put-attribute name="side" value="/WEB-INF/jsp/tiles/signinsignup.jsp"></put-attribute></definition><definition name="home" extends="template"><!--首页--><put-attribute name="content" value="/WEB-INF/jsp/home.jsp"></put-attribute></definition><definition name="spittles/list" extends="template"><!--特定spitter的spittle列表展示页--><put-attribute name="content" value="/WEB-INF/jsp/spittles/list.jsp"></put-attribute></definition><definition name="spittles/edit" extends="template"><!--特定spitter的表单提交页--><put-attribute name="content" value="/WEB-INF/jsp/spittles/edit.jsp"></put-attribute></definition><definition name="spittles/view" extends="template"><!--特定spitter的个人信息展示页--><put-attribute name="content" value="/WEB-INF/jsp/spittles/view.jsp"></put-attribute></definition></tiles-definitions>

 

可以看出,我们先定义了一个Template复用模板,在底下的首页跟其他特定的页面中,继承Template就可以复用到上面的公共片段了。

 

0 0
原创粉丝点击