當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring4 MVC + REST + List + Bootstrap 简单示例
生活随笔
收集整理的這篇文章主要介紹了
Spring4 MVC + REST + List + Bootstrap 简单示例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
本篇文章,我們將教會你通過eclipse創(chuàng)建并轉換為maven的web項目。通過spring4 mvc提供的REST方式將List對象中的值通JSTL 的c:forEach 標簽輸出到頁面中(本篇文章涉及到前面整合bootstrap的內(nèi)容)。
開發(fā)環(huán)境: 1. jdk 1.7 2.Maven 3.3.9 3.Eclipse Mars.1 4.Spring 4.2.1.RELEASE 5.Bootstrap
1.最終的目錄結構
2. 創(chuàng)建項目 Eclipse - File - New - Dynamic Web Project 輸入項目名稱,然后右擊該項目Configure-Convert to Maven project 并在WebContent/WEB-INF下新建一個web.xml文件 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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.thinkingingis</groupId><artifactId>SpringMvcListExample</artifactId><version>1.0.0-SNAPSHOT</version><packaging>war</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.2.1.RELEASE</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.1.3</version></dependency></dependencies><build><sourceDirectory>src</sourceDirectory><plugins><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.3</version><configuration><source>1.7</source><target>1.7</target></configuration></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>2.6</version><configuration><warSourceDirectory>WebContent</warSourceDirectory><failOnMissingWebXml>false</failOnMissingWebXml></configuration></plugin></plugins></build> </project>4.Spring MVC 4.1 Spring Controller-返回一個List對象 package org.thinkingingis.controller;import java.util.Map;import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; 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; import org.thinkingingis.service.HelloWorldService;import ch.qos.logback.classic.Logger;@Controller public class WelcomeController {private final Logger logger = (Logger) LoggerFactory.getLogger(WelcomeController.class);private HelloWorldService helloWorldService;@Autowiredpublic WelcomeController(HelloWorldService helloWorldService){this.helloWorldService = helloWorldService;}@RequestMapping(value = "/", method = RequestMethod.GET)public String index(Map<String, Object> model){logger.debug("index() method is executed");model.put("title", helloWorldService.getTitle(""));model.put("msg", helloWorldService.getDesc());return "index";}@RequestMapping(value = "/{name:.+}", method = RequestMethod.GET)public ModelAndView hello(@PathVariable("name") String name){logger.debug("hello() is executed - $name {}", name);ModelAndView model = new ModelAndView();model.setViewName("index");model.addObject("title", helloWorldService.getTitle(name));model.addObject("msg", helloWorldService.getDesc());model.addObject("list", helloWorldService.getList());return model; }} 4.2 Service 層,生成相關信息 package org.thinkingingis.service;import java.util.ArrayList; import java.util.List;import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils;import ch.qos.logback.classic.Logger;@Service public class HelloWorldService {private static final Logger logger = (Logger) LoggerFactory.getLogger(HelloWorldService.class);public String getDesc(){logger.debug("getDesc() is executed!");return "[Maven + Spring MVC + Bootstrap + List] Example";}public String getTitle(String name){logger.debug("getTitle() is executed! $name : {}", name);if(StringUtils.isEmpty(name)){return "Hello ThinkingInGIS";}else {return "Hello " + name;}}public List<String> getList(){List<String> list = new ArrayList<String>();list.add("List M");list.add("List A");list.add("List P");list.add("List S");list.add("List C");list.add("List A");list.add("List N");list.add("List T");list.add("List A");list.add("List L");list.add("List K");return list;} } 4.3 index.jsp展現(xiàn)層 JSP + JSTL + Bootstrap . /WEB-INF/views/jsp/index.jsp <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <!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>Maven + Spring mvc + Bootstrap</title><spring:url value="/resources/core/css/bootstrap.css" var="bootstrapCss"></spring:url> <link href="${bootstrapCss}" rel="stylesheet" /></head><body><nav class="navbar navbar-inverse navbar-fixed-top"><div class="container"><div class="navbar-header"><a class="navbar-brand" href="#">Spring Mvc List Example</a></div></div> </nav><div class="jumbotron"><div class="container"><h1>${title}</h1><p><c:if test="${not empty msg}">Hello ${msg}</c:if><c:if test="${empty msg}">Welcome ThinkingInGIS!</c:if></p><p><c:if test="${not empty list }"><ul><c:forEach var="list" items="${list }"><li>${list}</li></c:forEach></ul></c:if></p></div> </div><div class="container"><div class="row"><div class="col-md-4"><h2>Maps</h2><p>Maps</p></div><div class="col-md-4"><h2>Can</h2><p>Can</p></div><div class="col-md-4"><h2>Talk.</h2><p>Talk.</p></div></div><hr><footer><p>? ThinkingInGIS 2016</p></footer> </div><spring:url value="/resources/core/js/jquery.js" var="jquery" /> <spring:url value="/resources/core/js/bootstrap.js" var="bootstrapJs" /><script src="${jquery}"></script> <script src="${bootstrapJs}"></script></body> </html>4.4 配置日志信息:輸出日志到控制臺中 log.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration><appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"><layout class="ch.qos.logback.classic.PatternLayout"><Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</Pattern></layout></appender><logger name="org.springframework" level="debug"additivity="false"><appender-ref ref="STDOUT" /></logger><logger name="org.thinkingingis" level="debug"additivity="false"><appender-ref ref="STDOUT" /></logger><root level="debug"><appender-ref ref="STDOUT" /></root></configuration>5 以 XML 方式配置 spring 5.1 spring-core-config.xml <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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/contexthttp://www.springframework.org/schema/context/spring-context.xsd "><context:component-scan base-package="org.thinkingingis.service" /></beans>5.2 spring-mvc-config.xml <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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"/><property name="prefix" value="/WEB-INF/views/jsp/" /><property name="suffix" value=".jsp" /></bean><mvc:resources mapping="/resources/**" location="/resources/" /><mvc:annotation-driven /></beans>5.3 部署描述文件 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>Maven + Spring MVC Hello World + XML</display-name><description>Spring MVC web application</description><!-- For web context --><servlet><servlet-name>hello-dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/spring-mvc-config.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>hello-dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!-- For root context --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/spring-core-config.xml</param-value></context-param></web-app>6.部署項目 6.1 通過終端進入SpringMvcListExample 項目目錄下(windows系統(tǒng)通過cmd進入),輸入 mvn package后,出現(xiàn)"BUILD SUCCESS" 說明項目達成war包成功
6.2將項目target目錄下的SpringMvcListExample-1.0.0-SNAPSHOT.war 拷貝到 tomcat根目錄下的webapps文件夾下,啟動tomcat 輸入 http://localhost:8080/SpringMvcListExample/
輸入 http://localhost:8080/SpringMvcListExample/China
這也一個通過REST風格訪問的web程序就搭建好啦,頁面會遍歷List對象 源碼地址:https://github.com/ThinkingInGIS/SpringMvcListExample
歡迎大家關注:)
1.最終的目錄結構
2. 創(chuàng)建項目 Eclipse - File - New - Dynamic Web Project 輸入項目名稱,然后右擊該項目Configure-Convert to Maven project 并在WebContent/WEB-INF下新建一個web.xml文件 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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.thinkingingis</groupId><artifactId>SpringMvcListExample</artifactId><version>1.0.0-SNAPSHOT</version><packaging>war</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.2.1.RELEASE</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.1.3</version></dependency></dependencies><build><sourceDirectory>src</sourceDirectory><plugins><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.3</version><configuration><source>1.7</source><target>1.7</target></configuration></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>2.6</version><configuration><warSourceDirectory>WebContent</warSourceDirectory><failOnMissingWebXml>false</failOnMissingWebXml></configuration></plugin></plugins></build> </project>4.Spring MVC 4.1 Spring Controller-返回一個List對象 package org.thinkingingis.controller;import java.util.Map;import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; 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; import org.thinkingingis.service.HelloWorldService;import ch.qos.logback.classic.Logger;@Controller public class WelcomeController {private final Logger logger = (Logger) LoggerFactory.getLogger(WelcomeController.class);private HelloWorldService helloWorldService;@Autowiredpublic WelcomeController(HelloWorldService helloWorldService){this.helloWorldService = helloWorldService;}@RequestMapping(value = "/", method = RequestMethod.GET)public String index(Map<String, Object> model){logger.debug("index() method is executed");model.put("title", helloWorldService.getTitle(""));model.put("msg", helloWorldService.getDesc());return "index";}@RequestMapping(value = "/{name:.+}", method = RequestMethod.GET)public ModelAndView hello(@PathVariable("name") String name){logger.debug("hello() is executed - $name {}", name);ModelAndView model = new ModelAndView();model.setViewName("index");model.addObject("title", helloWorldService.getTitle(name));model.addObject("msg", helloWorldService.getDesc());model.addObject("list", helloWorldService.getList());return model; }} 4.2 Service 層,生成相關信息 package org.thinkingingis.service;import java.util.ArrayList; import java.util.List;import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils;import ch.qos.logback.classic.Logger;@Service public class HelloWorldService {private static final Logger logger = (Logger) LoggerFactory.getLogger(HelloWorldService.class);public String getDesc(){logger.debug("getDesc() is executed!");return "[Maven + Spring MVC + Bootstrap + List] Example";}public String getTitle(String name){logger.debug("getTitle() is executed! $name : {}", name);if(StringUtils.isEmpty(name)){return "Hello ThinkingInGIS";}else {return "Hello " + name;}}public List<String> getList(){List<String> list = new ArrayList<String>();list.add("List M");list.add("List A");list.add("List P");list.add("List S");list.add("List C");list.add("List A");list.add("List N");list.add("List T");list.add("List A");list.add("List L");list.add("List K");return list;} } 4.3 index.jsp展現(xiàn)層 JSP + JSTL + Bootstrap . /WEB-INF/views/jsp/index.jsp <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <!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>Maven + Spring mvc + Bootstrap</title><spring:url value="/resources/core/css/bootstrap.css" var="bootstrapCss"></spring:url> <link href="${bootstrapCss}" rel="stylesheet" /></head><body><nav class="navbar navbar-inverse navbar-fixed-top"><div class="container"><div class="navbar-header"><a class="navbar-brand" href="#">Spring Mvc List Example</a></div></div> </nav><div class="jumbotron"><div class="container"><h1>${title}</h1><p><c:if test="${not empty msg}">Hello ${msg}</c:if><c:if test="${empty msg}">Welcome ThinkingInGIS!</c:if></p><p><c:if test="${not empty list }"><ul><c:forEach var="list" items="${list }"><li>${list}</li></c:forEach></ul></c:if></p></div> </div><div class="container"><div class="row"><div class="col-md-4"><h2>Maps</h2><p>Maps</p></div><div class="col-md-4"><h2>Can</h2><p>Can</p></div><div class="col-md-4"><h2>Talk.</h2><p>Talk.</p></div></div><hr><footer><p>? ThinkingInGIS 2016</p></footer> </div><spring:url value="/resources/core/js/jquery.js" var="jquery" /> <spring:url value="/resources/core/js/bootstrap.js" var="bootstrapJs" /><script src="${jquery}"></script> <script src="${bootstrapJs}"></script></body> </html>4.4 配置日志信息:輸出日志到控制臺中 log.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration><appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"><layout class="ch.qos.logback.classic.PatternLayout"><Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</Pattern></layout></appender><logger name="org.springframework" level="debug"additivity="false"><appender-ref ref="STDOUT" /></logger><logger name="org.thinkingingis" level="debug"additivity="false"><appender-ref ref="STDOUT" /></logger><root level="debug"><appender-ref ref="STDOUT" /></root></configuration>5 以 XML 方式配置 spring 5.1 spring-core-config.xml <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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/contexthttp://www.springframework.org/schema/context/spring-context.xsd "><context:component-scan base-package="org.thinkingingis.service" /></beans>5.2 spring-mvc-config.xml <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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"/><property name="prefix" value="/WEB-INF/views/jsp/" /><property name="suffix" value=".jsp" /></bean><mvc:resources mapping="/resources/**" location="/resources/" /><mvc:annotation-driven /></beans>5.3 部署描述文件 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>Maven + Spring MVC Hello World + XML</display-name><description>Spring MVC web application</description><!-- For web context --><servlet><servlet-name>hello-dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/spring-mvc-config.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>hello-dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!-- For root context --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/spring-core-config.xml</param-value></context-param></web-app>6.部署項目 6.1 通過終端進入SpringMvcListExample 項目目錄下(windows系統(tǒng)通過cmd進入),輸入 mvn package后,出現(xiàn)"BUILD SUCCESS" 說明項目達成war包成功
6.2將項目target目錄下的SpringMvcListExample-1.0.0-SNAPSHOT.war 拷貝到 tomcat根目錄下的webapps文件夾下,啟動tomcat 輸入 http://localhost:8080/SpringMvcListExample/
輸入 http://localhost:8080/SpringMvcListExample/China
這也一個通過REST風格訪問的web程序就搭建好啦,頁面會遍歷List對象 源碼地址:https://github.com/ThinkingInGIS/SpringMvcListExample
(如遇到問題,請留言給作者,以便共同探討gis知識。thinkingingis@qq.com)
微信公眾號:ThinkingInGIS
歡迎大家關注:)
總結
以上是生活随笔為你收集整理的Spring4 MVC + REST + List + Bootstrap 简单示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用 Spring MVC 上传多文件到
- 下一篇: Spring 数据访问那些事儿(一)sp