改造继续之eclipse集成tomcat开发spring mvc项目配置一览

  在上一篇的环境配置中,你还只能基于maven开发一个javase的项目,如果要开发一个web项目,还得配置一下tomcat和spring mvc,集成一览表如下。

一:Tomcat安装

在.net web开发中,微软再一次向你展示了一站式马赛克配置,你只需要轻轻一点按钮,发射。。。一个带有bootstrap框架的页面就呈现在你的面前,在

java中就没有这么好的事情了,基本都是高清无码。

1. 下载地址

http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.5.20/bin/apache-tomcat-8.5.20.zip,目前tomcat最新版是 9.0 ,这里我选择的8.5版本。

改造继续之eclipse集成tomcat开发spring mvc项目配置一览

2. 配置环境变量

<1>  在windows平台下,一般会默认安装在:C:\Program Files\Apache Software Foundation\Tomcat 8.5 目录下,在你的windows服务列表中会有一个

   Apache Tomcat 8.5 Tomcat8服务项,这样就算安装完成了。

<2> centos平台下,因为项目需要部署在linux上,wget url,解压,最后执行startup.sh。

[root@localhost myapp]# cd tomcat
[root@localhost tomcat]# ls
bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
[root@localhost tomcat]# cd bin
[root@localhost bin]# ls
bootstrap.jar commons-daemon.jar daemon.sh setclasspath.sh startup.sh tool-wrapper.sh
catalina.bat commons-daemon-native.tar.gz digest.bat shutdown.bat tomcat-juli.jar version.bat
catalina.sh configtest.bat digest.sh shutdown.sh tomcat-native.tar.gz version.sh
catalina-tasks.xml configtest.sh setclasspath.bat startup.bat tool-wrapper.bat
[root@localhost bin]# ./startup.sh
Using CATALINA_BASE: /usr/myapp/tomcat
Using CATALINA_HOME: /usr/myapp/tomcat
Using CATALINA_TMPDIR: /usr/myapp/tomcat/temp
Using JRE_HOME: /usr/mysoft/java/jdk1.
Using CLASSPATH: /usr/myapp/tomcat/bin/bootstrap.jar:/usr/myapp/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@localhost bin]#

从上面可以看到,tomcat已经启动了,对了,汤姆猫的默认端口是8080,可以通过netstat -tln 验证一下,最后检查一下是否启动正常。

 [root@localhost bin]# netstat -tln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0.0.0.0: 0.0.0.0:* LISTEN
tcp 0.0.0.0: 0.0.0.0:* LISTEN
tcp 192.168.122.1: 0.0.0.0:* LISTEN
tcp 0.0.0.0: 0.0.0.0:* LISTEN
tcp 127.0.0.1: 0.0.0.0:* LISTEN
tcp 127.0.0.1: 0.0.0.0:* LISTEN
tcp6 ::: :::* LISTEN
tcp6 ::: :::* LISTEN
tcp6 0 0 :::8080 :::* LISTEN
tcp6 ::: :::* LISTEN
tcp6 ::: :::* LISTEN
tcp6 ::: :::* LISTEN
tcp6 127.0.0.1: :::* LISTEN

改造继续之eclipse集成tomcat开发spring mvc项目配置一览

二: eclipse 和 tomcat 的集成

两者集成起来,相对还是比较简单的,一般来说做下面三件事情就基本可以搞定了。

1.  在eclipse 的 windows -> references -> server  ->  enveriment runtime 中先指定你的tomcat版本,比如下图中的

apache tomcat 8.5版本,点击‘next’后指定一下tomcat的安装路径,指定你的jre运行版本即可。

改造继续之eclipse集成tomcat开发spring mvc项目配置一览改造继续之eclipse集成tomcat开发spring mvc项目配置一览

好了,接下来你可以新建一个 maven project -> 选择模式为:’‘maven-archetype-webapp’ ,然后填写好公司的域名和项目名称,最后就完成一个

web项目的创建,详细如下图:

改造继续之eclipse集成tomcat开发spring mvc项目配置一览改造继续之eclipse集成tomcat开发spring mvc项目配置一览

2.  右击web工程 -> Properties -> Java Build Path -> Libraries-> Add Library -> Server Runtime -> 添加tomcat ->

   切到order and export 勾选tomcat。

   当你终于创建好web工程之后,你会发现项目有‘错误信息’:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path。

改造继续之eclipse集成tomcat开发spring mvc项目配置一览

这是因为你的web工程还没有感知到jsp容器tomcat,这个时候你需要在 右击Web工程,在Properties面板中的Java Build Path添加tomcat的library。

详细步骤看一下标题即可,当一切做完之后,就能解决这个问题了。

改造继续之eclipse集成tomcat开发spring mvc项目配置一览

3. 添加jstl模板引擎

这个是可选项,如果你创建的 spring mvc工程运行的时候如果报错说缺少jstl模板,那么在pom.xml引用一下即可。

         <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.</version>
</dependency>

三:安装spring mvc

到目前为止,tomcat和eclipse的集成算是告一段落了,接下来要做的就是安装配置spring mvc。大家可以在maven仓库去拉一下即可,像

spring-core,spring-aop 这些必备依赖jar包会全部给你安装完毕。

改造继续之eclipse集成tomcat开发spring mvc项目配置一览改造继续之eclipse集成tomcat开发spring mvc项目配置一览

1. web.xml的配置

在src-> main -> webapp -> WEB-INF 下有一个web.xml文件,这个就相当于.net 中的web.config,在asp.net mvc 中最后是通过mvchandler进行了

请求接管,这种模式在spring mvc中同样适用,比如:接管的Servlet是DispatcherServlet,web.xml的详细配置如下:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
<display-name>Archetype Created Web Application</display-name> <!-- url请求拦截器 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!-- 字符集过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>

2. spring-servlet.xml 配置

我们知道spring其实就是一个bean的大容器,类的配置和管理都可以丢给spring,由于这里spring mvc采用的是‘注解模式’,所以要定义一下‘包‘

扫描的范围。这里文件名的定义要清楚一下:<$>-servlet.xml,其中的$就是web.xm中的<servlet-name>spring</servlet-name>的名称spring,最后把

此文件放置在WEB-INF文件夹下,方便tomcat容器加载的时候进行合并读取,详细配置如下:

 <?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:tx="http://www.springframework.org/schema/tx" 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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 配置扫描的包 -->
<context:component-scan base-package="com.datamip.qncrm.controller"></context:component-scan> <!-- 视图解析器 -->
<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>
<property name="suffix" value=".jsp"></property>
</bean> </beans>

3. 新建mvc的views文件夹存放所有view页面

在spring-servlet.xml文件配置节的‘视图解析器’的时候,可以看到所有的jsp页面都要放在views文件夹下,这里我新建一个index.jsp文件,详细内容如下:

 <%@ 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>首页</title>
</head>
<body>
<h1> SpringMVC 配置成功啦。。。。。</h1>
</body>
</html>

4. 新建mvc的controller控制器

在Java Resources 中的src/main/java 目录下,我可以新建一个HomeController.java,详细信息如下:

 package com.datamip.qncrm.controller;

 import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; @Controller
public class HomeController { //路由匹配,使用index.jsp进行页面渲染
@RequestMapping(value="/home/index",method=RequestMethod.GET)
public String Index() {
return "index";
}
}

好了,基本上spring mvc配置算是结束了,接下来我们在qncrm工程目录上 Run As -> Run As Server 执行,终于成功啦,也不容易哈~~~,相比.net 中的一站

式配置,确实难度上升了不少,不过有一句话说的好,硬是把一个做技术的程序员做成了没技术。如果大家理解asp.net mvc的urlRoutingModule 和MvcHandler,

原理都是一样的,希望本篇对你有帮助。ZIP文件下载

改造继续之eclipse集成tomcat开发spring mvc项目配置一览

上一篇:python重要模块之subprocess模块


下一篇:python常用模块及面向对象(一)