spring 配置一個基本的web項目

項目目錄html

wKiom1hWPj-h4xc4AAB0mKtCi-M124.jpg

記得要在Deployment Assembly裏將項目外部依賴包 WEB-INF/lib 加進去java

wKiom1hWPtGg4w6GAACfQlhp3No517.jpg

gradle項目,須要的包,其實這裏沒有用aop,若是用了,就須要其它的包了.還有第三方的包等等.
mysql

這裏的maven中央庫,用的是阿里雲的.web

build.gradle文件內容spring

apply plugin: 'java'

repositories {
    maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
}

configurations {
    //compile.exclude module: 'commons-logging'
}

dependencies {	compile group: 'jstl', name: 'jstl', version: '1.2'    compile("org.springframework:spring-context:${springVersion}")
    compile("org.springframework:spring-jdbc:${springVersion}")
    compile group: 'org.springframework', name: 'spring-web', version: "${springVersion}"
    compile group: 'org.springframework', name: 'spring-webmvc', version: "${springVersion}"
		
	compile group: 'org.apache.commons', name: 'commons-dbcp2', version: '2.1.1'
	compile group: 'commons-lang', name: 'commons-lang', version: '2.6'
	
	compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.3'
	compile group: 'org.logback-extensions', name: 'logback-ext-spring', version: '0.1.2'
	compile group: 'org.slf4j', name: 'jcl-over-slf4j', version: '1.7.12'
		
    testCompile 'junit:junit:4.12'
    
}

gradle.properties文件內容sql

springVersion=4.3.3.RELEASE

maven的包
數據庫

pom.xml文件內容apache

<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>com.example</groupId>
	<artifactId>weblearn</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>weblearn</name>
	<url>http://maven.aliyun.com/nexus/content/groups/public/</url>

	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<excludes>
						<exclude>commons-logging:commons-logging</exclude>
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<org.springframework.version>4.3.3.RELEASE</org.springframework.version>
		<org.slf4j.version>1.7.21</org.slf4j.version>
		<org.junit.version>4.12</org.junit.version>
	</properties>

	<dependencies>
				

		<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/javax.servlet.jsp.jstl-api -->
		<dependency>
			<groupId>javax.servlet.jsp.jstl</groupId>
			<artifactId>javax.servlet.jsp.jstl-api</artifactId>
			<version>1.2.1</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/junit/junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${org.junit.version}</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>
		<!-- JDBC Data Access Library (depends on spring-core, spring-beans, spring-context, 
			spring-tx) Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>

		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.1.3</version>
		</dependency>
		<dependency>
			<groupId>org.logback-extensions</groupId>
			<artifactId>logback-ext-spring</artifactId>
			<version>0.1.2</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>1.7.12</version>
		</dependency>
	</dependencies>
</project>

web.xml文件,這裏用了Dispatchservlet,用了logback,還自定義了webAppRootKey值,這裏沒有明確指明spring的應用上下文,因而用了默認的文件,即同web.xml目錄下的weblearn3-servlet.xmlapi

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
	
    <servlet>
		<servlet-name>weblearn3</servlet-name>
			<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
			</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>weblearn3</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<context-param>
		<param-name>webAppRootKey</param-name>
		<param-value>weblearn3.root</param-value>
	</context-param>
	<context-param>
		<param-name>logbackConfigLocation</param-name>
		<param-value>/WEB-INF/config/logback3.xml</param-value>
	</context-param>
	<listener>
		<listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class>
	</listener>
</web-app>

與web.xml同一目錄下的weblearn3-servlet.xml文件,這裏用了,引入其它的配置文件,如數據庫配置文件,還用了自動掃描,還使用了JSTL,指明解析的view名對應的物理文件在/WEB-INF/views/目錄下,文件後綴名爲.jsp
spring-mvc

<?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:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:security="http://www.springframework.org/schema/security" 
	xsi:schemaLocation="
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans	http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
	">
	
	<import resource="datasource.xml"/>
 	<mvc:resources mapping="/resources/**" location="/resources/" /> 
	<mvc:annotation-driven/>
	<context:component-scan	base-package="com.example.weblearn3"></context:component-scan>
		
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<constructor-arg ref="dataSource" />
	</bean>
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/views/"/>
		<property name="suffix" value=".jsp"/>
	</bean>	
	
</beans>

與web.xml同目錄下的datasource.xml文件,這裏用了dbcp2

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    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.xsd 
	">
                        
	<bean id="dataSource" 	class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/myweibo?serverTimezone=UTC" />
		<property name="username" value="root" />
		<property name="password" value="123456" />
		<property name="initialSize" value="5" />
<!-- 		<property name="maxActive" value="10" /> -->
	</bean>

</beans>

配置一個controller,HomeController.java文件,這裏的home方法沒有明確指明value值,因此默認該方法就是項目的主頁.也能夠指明@RequestMapping(value = "/", method = RequestMethod.GET),可是沒有必要.

package com.example.weblearn3.controller;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;



@Controller
@RequestMapping({"/", "/homepage","index.html"})
public class HomeController {
	
	private Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	@RequestMapping(method = RequestMethod.GET)
	public String home() {
		
        try {
            int a = 1/0;
        }catch (Exception e){
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            logger.error("Main錯誤:" + sw.toString());
        }
		System.out.println("home");
		return "home";
	}
	
}

訪問http://localhost:8080/weblearn3/便可

相關文章
相關標籤/搜索