在上一篇的环境配置中,你还只能基于 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 版本。
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.8
- 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:111 0.0.0.0:* LISTEN
- tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
- tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN
- tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
- tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
- tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
- tcp6 0 0 :::8009 :::* LISTEN
- tcp6 0 0 :::111 :::* LISTEN
- tcp6 0 0 :::8080 :::* LISTEN
- tcp6 0 0 :::22 :::* LISTEN
- tcp6 0 0 ::1:631 :::* LISTEN
- tcp6 0 0 ::1:25 :::* LISTEN
- tcp6 0 0 127.0.0.1:8005 :::* LISTEN
二: eclipse 和 tomcat 的集成
两者集成起来,相对还是比较简单的,一般来说做下面三件事情就基本可以搞定了。
1. 在 eclipse 的 windows -> references -> server -> enveriment runtime 中先指定你的 tomcat 版本,比如下图中的
apache tomcat 8.5 版本,点击'next'后指定一下 tomcat 的安装路径,指定你的 jre 运行版本即可。
好了,接下来你可以新建一个 maven project -> 选择模式为:''maven-archetype-webapp' ,然后填写好公司的域名和项目名称,最后就完成一个
web 项目的创建,详细如下图:
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。
这是因为你的 web 工程还没有感知到 jsp 容器 tomcat,这个时候你需要在 右击 Web 工程,在 Properties 面板中的 Java Build Path 添加 tomcat 的 library。
详细步骤看一下标题即可,当一切做完之后,就能解决这个问题了。
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.2</version>
- </dependency>
三:安装 spring mvc
到目前为止,tomcat 和 eclipse 的集成算是告一段落了,接下来要做的就是安装配置 spring mvc。大家可以在 maven 仓库去拉一下即可,像
spring-core,spring-aop 这些必备依赖 jar 包会全部给你安装完毕。
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 文件下载
来源: http://www.cnblogs.com/huangxincheng/p/7441221.html