2014年7月14日 星期一

maven學習 十二 : SpringMVC + Tiles + log4j

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>tw.ocean.englishProject</groupId>
  <artifactId>englishProject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
 
    <!-- 依賴,就是程式中會用到的jar -->
  <dependencies>  
<!-- Spring Core Bean AOP Expression Context-->
<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.2.9.RELEASE</version>
    </dependency>
   <!-- Spring MVC -->
    <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.9.RELEASE</version>
 </dependency>

 <!-- log工具  spring 需要log4j + slf4j-->
 <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
 </dependency>
 <dependency>                          
       <groupId>org.slf4j</groupId>
      <artifactId>jcl-over-slf4j</artifactId>
      <version>1.7.7</version>
 </dependency>
 <dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.2</version>
 </dependency>
 <!-- 樣板框架 -->
 <dependency>
       <groupId>org.apache.tiles</groupId>
      <artifactId>tiles-extras</artifactId>
      <version>2.2.2</version>
    </dependency>    
    <dependency>
       <groupId>org.apache.tiles</groupId>
      <artifactId>tiles-servlet</artifactId>
      <version>2.2.2</version>
 </dependency>
 <dependency>
   <groupId>org.apache.tiles</groupId>
     <artifactId>tiles-jsp</artifactId>
     <version>2.2.2</version>
 </dependency>
 <!--  -->
 <dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
 </dependency>

</dependencies>
</project>




web.xml

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>englishProject</display-name>
 
<!-- Log4j -->
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<!-- 半小時會掃描一次 log4j.properties檔,做熱部署-->
<context-param>
   <param-name>log4jRefreshInterval</param-name>
   <param-value>1800000</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<!-- Spring MVC HTTP 請求入口 ,含 DefaultAnnotationHandlerMapping 、 BeanNameUrlHandlerMapping-->
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <init-param>
    <!-- 告訴IOC容器xml所在來生成Bean實體 -->
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-context.xml</param-value>
    </init-param>
    <!-- 啟動順序,1隨servlet容器一起啟動 -->
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <!-- 處理所有的請求 -->
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

spring-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<import resource="classpath*:/META-INF/spring/spring-webmvc.xml" />
</beans>


spring-webmvc.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"
xmlns:p="http://www.springframework.org/schema/p"
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-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<!-- 靜態資源處理,聲明  DispatccherServlet不要處理的路徑資源-->
<mvc:resources location="/resources/" mapping="/resources/**"/>

<!-- 掃描package 檢視特殊註解標記   執行自動Bean注入 -->
<context:component-scan base-package="front.*" ></context:component-scan>

<!-- 增強annotation Spring MVC 用於  DefaultAnnotationHandlerMapping--> 
<mvc:annotation-driven></mvc:annotation-driven>

<!-- MVC OUTPUT -->
<!-- Tiles -->
<bean id="tilesConfigure" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles-global.xml</value>
<!--
<value>/WEB-INF/tiles-front.xml</value>
<value>/WEB-INF/tiles-back.xml</value>
-->
</list>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.tiles2.TilesView</value>
</property>
</bean>

<!-- ContentNegotiatingViewResolver 支援多格式輸出-->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="json" value="application/json" />
</map>
</property>
<property name="viewResolvers">
<list>
<ref bean="viewResolver" />
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</list>
</property>
<!-- 是否忽略HttpHeader:Accept -->
<property name="ignoreAcceptHeader" value="false" />
</bean>
</beans>


log4j.properties

#log4j設定檔,此檔只要放到WEB-INF或META-INF中,spring中即可運作

# Root logger option
log4j.rootLogger=INFO,BreadCrumbs

# Console輸出 與 輸出格式
log4j.appender.BreadCrumbs=org.apache.log4j.ConsoleAppender
log4j.appender.BreadCrumbs.Target=System.out
log4j.appender.BreadCrumbs.layout=org.apache.log4j.PatternLayout
log4j.appender.BreadCrumbs.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

GlobalController.java
package front.controller;

import java.util.Map;

import javassist.bytecode.stackmap.TypeData.ClassName;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class GlobalController  {
private static final long serialVersionUID = 1L;

private static Logger logger = Logger.getLogger(ClassName.class);
     

    public GlobalController() {    
        super();
    }

    /**
     * 處理轉發首頁
     * @param model
     * @return ModelAndView
     */
    @RequestMapping(value= "/"  ,method = RequestMethod.GET)
    public ModelAndView  dispatcherDefaultPage(){
     logger.info("in dispatcherHome");
          //Tiles  
     ModelAndView view = new ModelAndView("homeView");  //將導出homeView
    return view;
    }
   
    @RequestMapping(value= "/home"  ,method = RequestMethod.GET)
    public ModelAndView  dispatcherHome(){    
    return dispatcherDefaultPage();
    }
}


tiles-global.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<!-- 整體環境 -->
<tiles-definitions>
<!-- 整體環境統一基版 -->
    <definition name="base.definition.global"
        template="/view/tiles-templet/global-layout.jsp">
        <put-attribute name="title" value="" />
        <put-attribute name="header" value="/view/tiles-templet/global-header.jsp" />
        <put-attribute name="menu" value="/view/tiles-templet/global-menu.jsp" />
        <put-attribute name="body" value="" />
        <put-attribute name="footer" value="/view/tiles-templet/global-footer.jsp" />
    </definition>
<!-- 
    <definition name="error" extends="base.definition.global">
        <put-attribute name="title" value="error" />
        <put-attribute name="body" value="/view/global-error-body.jsp" />
    </definition>
-->    
    <definition name="homeView" extends="base.definition.global">
        <put-attribute name="title" value="home" />
        <put-attribute name="body" value="/view/global-index-body.jsp" />
    </definition>
</tiles-definitions>


global-layout.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><tiles:insertAttribute name="title" ignore="true" /></title>
</head>
<body>
<tiles:insertAttribute name="title" ignore="true" />
<tiles:insertAttribute name="menu" />
<tiles:insertAttribute name="body" />
<tiles:insertAttribute name="footer" />
</body>
</html>


global-header.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<header>Head</header>


global-menu.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<nav>
<ul>
<li>閱讀頁</li>
<li>狀態頁</li>
</ul>
</nav>


global-footer.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<footer>頁腳</footer>


global-index-body.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<article>
body body body 真木陽子
</article>

沒有留言:

張貼留言