项目目录
在赤坎等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都做网站、成都网站设计、成都外贸网站建设 网站设计制作按需设计网站,公司网站建设,企业网站建设,品牌网站设计,成都全网营销推广,外贸营销网站建设,赤坎网站建设费用合理。记得要在Deployment Assembly里将项目外部依赖包 WEB-INF/lib 加进去
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/即可
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章题目:spring配置一个基本的web项目-创新互联
标题链接:https://www.cdcxhl.com/article12/jojdc.html
成都网站建设公司_创新互联,为您提供小程序开发、网站制作、商城网站、网站设计、网站收录、网站内链
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联