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>


maven學習 十一 : Maven Srping MVC


Maven 配置:
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>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>
<!-- Spring MVC -->
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <init-param>    
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-context.xml</param-value>
    </init-param>  
    <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


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">





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">


<!-- 掃描pojo查看註解來執行自動注入 -->
<context:component-scan base-package="front.*" ></context:component-scan>
<!--Request 請求URL映射-->
<mvc:annotation-driven></mvc:annotation-driven>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/"/>
    <property name="suffix" value=".jsp"/>
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>
</beans>


ReadController  

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;

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

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

    public ReadController() {    
        super();
        logger.info("YYYYYYYYYYYYYYYYYYY");
    }

    @RequestMapping({"/" , "/home"})
    public String test(Map<String, Object> model){
     logger.info("HOME HOME HOME HOME HOME");
    return "home";
    }


}