您好,登錄后才能下訂單哦!
項(xiàng)目目錄
記得要在Deployment Assembly里將項(xiàng)目外部依賴包 WEB-INF/lib 加進(jìn)去
gradle項(xiàng)目,需要的包,其實(shí)這里沒(méi)有用aop,如果用了,就需要其它的包了.還有第三方的包等等.
這里的maven中央庫(kù),用的是阿里云的.
build.gradle文件內(nèi)容
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文件內(nèi)容
springVersion=4.3.3.RELEASE
maven的包
pom.xml文件內(nèi)容
<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值,這里沒(méi)有明確指明spring的應(yīng)用上下文,于是用了默認(rèn)的文件,即同web.xml目錄下的weblearn3-servlet.xml
<?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文件,這里用了,引入其它的配置文件,如數(shù)據(jù)庫(kù)配置文件,還用了自動(dòng)掃描,還使用了JSTL,指明解析的view名對(duì)應(yīng)的物理文件在/WEB-INF/views/目錄下,文件后綴名為.jsp
<?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>
配置一個(gè)controller,HomeController.java文件,這里的home方法沒(méi)有明確指明value值,所以默認(rèn)該方法就是項(xiàng)目的主頁(yè).也可以指明@RequestMapping(value = "/", method = RequestMethod.GET),但是沒(méi)有必要.
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錯(cuò)誤:" + sw.toString()); } System.out.println("home"); return "home"; } }
訪問(wèn)http://localhost:8080/weblearn3/即可
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。