Eclipse·Maven·構建SpringMVC簡單工程-3

  1. 接上篇,咱們Maven的基本框架已經搭建完成。後續加入Spring MVC的基礎依賴。完整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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>net.chinaos.spring387</groupId>
	<artifactId>springmvc</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>springmvc Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>servletapi</groupId>
			<artifactId>servletapi</artifactId>
			<version>2.4</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.0.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.0.5.RELEASE</version>
		</dependency>
			<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.7</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.7</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.12</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<finalName>springmvc</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.4</version>

			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<encoding>utf8</encoding>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
  1. 新建類SpringMVC
package net.chinaos.springmvc.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("springmvc")
public class SpringMVC {

	@RequestMapping("test")
	public String page(){
		return "test";
	}
}
  1. 在src/main/resources中創建文件夾log4j spring 存在日誌配置文件;spring-mvc配置文件
  2. log4j內容以下
log4j.rootLogger=info,conApp,errorLog,infoLog
log4j.appender.conApp=org.apache.log4j.ConsoleAppender
log4j.appender.conApp.layout=org.apache.log4j.PatternLayout
log4j.appender.conApp.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-16t %-5p %c{1} - %m%n
log4j.appender.errorLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.errorLog.Threshold=ERROR
log4j.appender.errorLog.File=/home/data/logs/saas/api/log4e.log
log4j.appender.errorLog.layout=org.apache.log4j.PatternLayout
log4j.appender.errorLog.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-16t %-5p %c - %m%n
log4j.appender.infoLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.infoLog.Threshold=info
log4j.appender.infoLog.File=/home/data/logs/saas/api/log4i.log
log4j.appender.infoLog.layout=org.apache.log4j.PatternLayout
log4j.appender.infoLog.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-16t %-5p %c - %m%n
log4j.category.org.nutz=info
  1. spring-mvc.xml內容以下
<?xml version="1.1" 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:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">	
	<!-- 自動掃描且只掃描@Controller -->
	<context:component-scan base-package="net.chinaos" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
		<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
	</context:component-scan>	
	<mvc:annotation-driven>
		<mvc:message-converters register-defaults="true">
			<!-- 將StringHttpMessageConverter的默認編碼設爲UTF-8 -->
			<bean class="org.springframework.http.converter.StringHttpMessageConverter">
		    	<constructor-arg value="UTF-8" />
			</bean>
  		</mvc:message-converters>
	</mvc:annotation-driven>	
	<!-- 定義JSP文件的位置 --> 
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"/>
		<property name="suffix" value=".jsp"/>
	</bean>			
	<!-- 容器默認的DefaultServletHandler處理 全部靜態內容與無RequestMapping處理的URL-->	
	<mvc:default-servlet-handler/>	
</beans>
  1. 在WEB-INF中新建文件夾views,且在文件夾中新建文件test.jspgit

  2. 最後運行,截圖以下:
    輸入圖片說明web

  3. 詳細代碼見: https://git.oschina.net/sevencm/springmvcspring

相關文章
相關標籤/搜索