spring 配置一个基本的web项目

发布时间:2020-07-16 15:55:59 作者:wangtenfee
来源:网络 阅读:908

项目目录

spring 配置一个基本的web项目

记得要在Deployment Assembly里将项目外部依赖包 WEB-INF/lib 加进去

spring 配置一个基本的web项目

gradle项目,需要的包,其实这里没有用aop,如果用了,就需要其它的包了.还有第三方的包等等.

这里的maven中央库,用的是阿里云的.

build.gradle文件内容

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文件内容

springVersion=4.3.3.RELEASE

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>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.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文件,这里用了,引入其它的配置文件,如数据库配置文件,还用了自动扫描,还使用了JSTL,指明解析的view名对应的物理文件在/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>

配置一个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/即可

推荐阅读:
  1. 【总结】基于Spring LDAP和Spring Security的用户认证和权限控制Web实现
  2. 怎样快速学Spring Boot

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring web

上一篇:Shell脚本之case语句与循环语句

下一篇:oracle获取表或视图的字段名、数据类型、注释

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》