本篇文章,我们将教会你通过eclipse创建并转换为maven的web项目。通过spring4 mvc提供的REST方式将List对象中的值通JSTL 的
c:forEach 标签输出到页面中(本篇文章涉及到前面整合bootstrap的内容)。
开发环境:
1. jdk 1.7
2.
Maven 3.3.9
3.
Eclipse Mars.1
4.Spring 4.2.1.RELEASE
5.Bootstrap
1.最终的目录结构
2.
创建项目
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;
@Autowired
public 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展现层 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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://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/javaee
http://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系统通过cmd进入),输入 mvn package后,出现"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对象
(如遇到问题,请留言给作者,以便共同探讨gis知识。thinkingingis@qq.com)
微信公众号:ThinkingInGIS