springmvc +bootstrap创建简单web项目
生活随笔
收集整理的這篇文章主要介紹了
springmvc +bootstrap创建简单web项目
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本篇文章,我們會教你通過maven和spring mvc + bootstrap創建一個Java web項目的歡迎頁。關于maven如何創建Java web項目,上一篇文章已經作了說明。
開始構建項目之前,有必要說明一下:由于maven的中央倉庫太難用了,我們先更改一下maven的鏡像,這里用到的是阿里云的鏡像。在maven根目錄下 conf文件夾下的 setting.xml文件,將其打開,在<mirrors>節點下增加如下代碼
<mirror><id>alimaven</id><name>aliyun maven</name><url>http://maven.aliyun.com/nexus/content/groups/public</url><mirrorOf>central</mirrorOf></mirror>
如下圖所示:
開發環境: 1.jdk 1.7 2.Maven 3.3.9 3.Eclipse Mars.1 4.Spring 4.2.1.RELEASE 5.Bootstrap 1.通過maven構建項目 mvn archetype:generate -DgroupId=org.thinkingingis -DartifactId=SpringmvcBootstrap -Dversion=1.0.0-SNAPSHOP -DarchetypeArtifactId=maven-archetype-webapp 2.目錄結構及文件
3. 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>org.thinkingingis</groupId><artifactId>SpringmvcBootstrap</artifactId><packaging>war</packaging><version>1.0.0-SNAPSHOT</version><name>SpringmvcBootstrap Maven Webapp</name><url>http://maven.apache.org</url><properties><jdk.version>1.7</jdk.version><spring.version>4.1.1.RELEASE</spring.version><jstl.version>1.2</jstl.version><junit.version>4.11</junit.version><logback.version>1.0.13</logback.version><jcl-over-slf4j.version>1.7.5</jcl-over-slf4j.version></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><!-- https://mvnrepository.com/artifact/javax.servlet/jstl --><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency></dependencies><build><finalName>SpringmvcBootstrap</finalName><plugins><!-- Eclipse project --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-eclipse-plugin</artifactId><version>2.9</version><configuration><!-- Always download and attach dependencies source code --><downloadSources>true</downloadSources><downloadJavadocs>false</downloadJavadocs><!-- Avoid type mvn eclipse:eclipse -Dwtpversion=2.0 --><wtpversion>2.0</wtpversion></configuration></plugin><!-- Set JDK Compiler Level --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>${jdk.version}</source><target>${jdk.version}</target></configuration></plugin></plugins></build> </project> 4. HelloController.java package org.thinkingingis.controller;import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView;@Controller public class HelloController {@RequestMapping(value = "/", method = RequestMethod.GET)public String printWelcome(ModelMap model){model.addAttribute("message", "Hello ThinkingInGIS by Spring MVC ");return "Hello ThinkingInGIS";}@RequestMapping(value="/{name}", method = RequestMethod.GET)public ModelAndView hello(@PathVariable("name") String name){ModelAndView model = new ModelAndView();model.setViewName("index");model.addObject("msg", name);return model;} } 5.JSP文件 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Spring MVC + Bootstrap</title><link href="./resources/bootstrap.min.css" rel="stylesheet" /></head><nav class="navbar navbar-inverse navbar-fixed-top"><div class="container"><div class="navbar-header"><a class="navbar-brand" href="#">Spring MVC + Bootstrap</a></div></div> </nav><div class="jumbotron"><div class="container"><h1>${title}</h1><p><c:if test="${not empty name}">Hello ${name}</c:if><c:if test="${empty name}">Welcome ThinkingInGIS !</c:if></p><p><a class="btn btn-primary btn-lg" href="#" role="button">Maps Talk</a></p></div> </div><div class="container"><div class="row"><div class="col-md-4"><h2>Maps</h2><p>maps</p><p><a class="btn btn-default" href="#" role="button">Maps</a></p></div><div class="col-md-4"><h2>can</h2><p>can</p><p><a class="btn btn-default" href="#" role="button">can</a></p></div><div class="col-md-4"><h2>Talk</h2><p>Talk</p><p><a class="btn btn-default" href="#" role="button">Talk</a></p></div></div><hr><footer><p>? ThinkingInGIS 2016</p></footer> </div> <script type="text/javascript" src="./resources/jquery.js"></script> <script type="text/javascript" src="./resources/bootstrap.min.js"></script></body> </html> 6.spring-web-servlet.xml (spring的配置文件) <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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="org.thinkingingis.controller" /><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property><property name="prefix"><value>/</value></property><property name="suffix"><value>.jsp</value></property></bean><mvc:resources mapping="/resources/**" location="/resources/" /><mvc:default-servlet-handler></mvc:default-servlet-handler></beans>web.xml (部署描述文件) <web-app 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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version="2.5"><display-name>Spring webapp</display-name><servlet><servlet-name>spring-web</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>spring-web</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app> 7.將項目打包,并部署 在SpringmvcBootstrap文件夾下,通過命令行輸入: mvn package 一個WAR包文件會出現在target下,將該文件復制到tomcat根目錄/bin/webapp文件夾下即可,然后啟動tomcat 輸入 localhost:8080/SpringmvcBootstrap/ 看看會發生什么吧。
至此,一個通過簡單的spring mvc + bootstrap的小程序就完成啦。 源碼地址:https://github.com/ThinkingInGIS/SpringmvcBootstrap 至此,一個通過maven創建的java web程序就完成啦。
如果你覺得本文對你有幫助,是支持贊賞的哦:)
(如遇到問題,請留言給作者,以便共同探討gis知識。thinkingingis@qq.com)
微 信公眾號:ThinkingInGIS
開發環境: 1.jdk 1.7 2.Maven 3.3.9 3.Eclipse Mars.1 4.Spring 4.2.1.RELEASE 5.Bootstrap 1.通過maven構建項目 mvn archetype:generate -DgroupId=org.thinkingingis -DartifactId=SpringmvcBootstrap -Dversion=1.0.0-SNAPSHOP -DarchetypeArtifactId=maven-archetype-webapp 2.目錄結構及文件
3. 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>org.thinkingingis</groupId><artifactId>SpringmvcBootstrap</artifactId><packaging>war</packaging><version>1.0.0-SNAPSHOT</version><name>SpringmvcBootstrap Maven Webapp</name><url>http://maven.apache.org</url><properties><jdk.version>1.7</jdk.version><spring.version>4.1.1.RELEASE</spring.version><jstl.version>1.2</jstl.version><junit.version>4.11</junit.version><logback.version>1.0.13</logback.version><jcl-over-slf4j.version>1.7.5</jcl-over-slf4j.version></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><!-- https://mvnrepository.com/artifact/javax.servlet/jstl --><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency></dependencies><build><finalName>SpringmvcBootstrap</finalName><plugins><!-- Eclipse project --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-eclipse-plugin</artifactId><version>2.9</version><configuration><!-- Always download and attach dependencies source code --><downloadSources>true</downloadSources><downloadJavadocs>false</downloadJavadocs><!-- Avoid type mvn eclipse:eclipse -Dwtpversion=2.0 --><wtpversion>2.0</wtpversion></configuration></plugin><!-- Set JDK Compiler Level --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>${jdk.version}</source><target>${jdk.version}</target></configuration></plugin></plugins></build> </project> 4. HelloController.java package org.thinkingingis.controller;import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView;@Controller public class HelloController {@RequestMapping(value = "/", method = RequestMethod.GET)public String printWelcome(ModelMap model){model.addAttribute("message", "Hello ThinkingInGIS by Spring MVC ");return "Hello ThinkingInGIS";}@RequestMapping(value="/{name}", method = RequestMethod.GET)public ModelAndView hello(@PathVariable("name") String name){ModelAndView model = new ModelAndView();model.setViewName("index");model.addObject("msg", name);return model;} } 5.JSP文件 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Spring MVC + Bootstrap</title><link href="./resources/bootstrap.min.css" rel="stylesheet" /></head><nav class="navbar navbar-inverse navbar-fixed-top"><div class="container"><div class="navbar-header"><a class="navbar-brand" href="#">Spring MVC + Bootstrap</a></div></div> </nav><div class="jumbotron"><div class="container"><h1>${title}</h1><p><c:if test="${not empty name}">Hello ${name}</c:if><c:if test="${empty name}">Welcome ThinkingInGIS !</c:if></p><p><a class="btn btn-primary btn-lg" href="#" role="button">Maps Talk</a></p></div> </div><div class="container"><div class="row"><div class="col-md-4"><h2>Maps</h2><p>maps</p><p><a class="btn btn-default" href="#" role="button">Maps</a></p></div><div class="col-md-4"><h2>can</h2><p>can</p><p><a class="btn btn-default" href="#" role="button">can</a></p></div><div class="col-md-4"><h2>Talk</h2><p>Talk</p><p><a class="btn btn-default" href="#" role="button">Talk</a></p></div></div><hr><footer><p>? ThinkingInGIS 2016</p></footer> </div> <script type="text/javascript" src="./resources/jquery.js"></script> <script type="text/javascript" src="./resources/bootstrap.min.js"></script></body> </html> 6.spring-web-servlet.xml (spring的配置文件) <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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="org.thinkingingis.controller" /><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property><property name="prefix"><value>/</value></property><property name="suffix"><value>.jsp</value></property></bean><mvc:resources mapping="/resources/**" location="/resources/" /><mvc:default-servlet-handler></mvc:default-servlet-handler></beans>web.xml (部署描述文件) <web-app 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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version="2.5"><display-name>Spring webapp</display-name><servlet><servlet-name>spring-web</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>spring-web</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app> 7.將項目打包,并部署 在SpringmvcBootstrap文件夾下,通過命令行輸入: mvn package 一個WAR包文件會出現在target下,將該文件復制到tomcat根目錄/bin/webapp文件夾下即可,然后啟動tomcat 輸入 localhost:8080/SpringmvcBootstrap/ 看看會發生什么吧。
至此,一個通過簡單的spring mvc + bootstrap的小程序就完成啦。 源碼地址:https://github.com/ThinkingInGIS/SpringmvcBootstrap 至此,一個通過maven創建的java web程序就完成啦。
如果你覺得本文對你有幫助,是支持贊賞的哦:)
(如遇到問題,請留言給作者,以便共同探討gis知識。thinkingingis@qq.com)
微 信公眾號:ThinkingInGIS
總結
以上是生活随笔為你收集整理的springmvc +bootstrap创建简单web项目的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: maven + spring mvc 创
- 下一篇: Springmvc Bootstrap