改造继续之eclipse集成tomcat开发spring mvc项目配置一览,tomcatmvc
在上一篇的环境安排中,你还只好遵照maven开发一个javase的类型,假如要付出一个web项目,还得配置一下tomcat和spring
mvc,集成一览表如下。
一:Tomcat安装
在.net
web开发中,微软再三次向您出示了一站式马赛克配置,你只须要轻装一点按钮,发射。。。一个含有bootstrap框架的页面就表以后你的前边,在
java中就从未那样好的事务了,基本都以高清无码。
- 下载地址
http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.5.20/bin/apache-tomcat-8.5.20.zip,目前tomcat最新版是
9.0 ,这里自己接纳的8.5本子。
- 配备环境变量
<1> 在windows平台下,一般会私行认同安装在:C:\Program
Files\Apache Software Foundation\汤姆cat 8.5
目录下,在您的windows服务列表中会有一个
Apache 汤姆cat 8.5 汤姆cat8服务项,那样就是安装到位了。
<2> centos平台下,因为项目须求配备在linux上,wget
url,解压,最终执行startup.sh。
[[email protected] myapp]# cd tomcat
[[email protected] tomcat]# ls
bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
[[email protected] tomcat]# cd bin
[[email protected] 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
[[email protected] 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.
[[email protected] bin]#
从地点可以看看,tomcat已经启动了,对了,汤姆猫的私下认同端口是8080,可以透过netstat
-tln 验证一下,最终检查一下是还是不是启动健康。
1 [[email protected] bin]# netstat -tln
2 Active Internet connections (only servers)
3 Proto Recv-Q Send-Q Local Address Foreign Address State
4 tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
5 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
6 tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN
7 tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
8 tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
9 tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
10 tcp6 0 0 :::8009 :::* LISTEN
11 tcp6 0 0 :::111 :::* LISTEN
12 tcp6 0 0 :::8080 :::* LISTEN
13 tcp6 0 0 :::22 :::* LISTEN
14 tcp6 0 0 ::1:631 :::* LISTEN
15 tcp6 0 0 ::1:25 :::* LISTEN
16 tcp6 0 0 127.0.0.1:8005 :::* LISTEN
二: eclipse 和 tomcat 的集成
两者集成起来,相对仍旧相比不难的,一般的话做下边三件业务就着力得以化解了。
- 在eclipse 的 windows -> references -> server ->
enveriment runtime 中先指定你的tomcat版本,比如下图中的
apache tomcat
8.5版本,点击‘next’后指定一下tomcat的装置路径,指定你的jre运行版本即可。
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。
详尽步骤看一下标题即可,当一切做完事后,就能消除这几个题材了。
- 添加jstl模板引擎
这么些是可采取,倘诺您成立的 spring
mvc工程运行的时候要是报错说缺乏jstl模板,那么在pom.xml引用一下即可。
1 <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
2 <dependency>
3 <groupId>javax.servlet</groupId>
4 <artifactId>jstl</artifactId>
5 <version>1.1.2</version>
6 </dependency>
三:安装spring mvc
到近年来截至,tomcat和eclipse的并轨算是告一段落了,接下去要做的就是安装配置spring
mvc。大家可以在maven仓库去拉一下即可,像
spring-core,spring-aop 这个不可或缺正视jar包会全部给您安装收尾。
- web.xml的配置
在src-> main -> webapp -> WEB-INF
下有一个web.xml文件,这么些就相当于.net 中的web.config,在asp.net mvc
中最后是由此mvchandler进行了
请求接管,那种方式在spring
mvc中千篇一律适用,比如:接管的Servlet是DispatcherServlet,web.xml的详实布置如下:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
5 <display-name>Archetype Created Web Application</display-name>
6
7 <!-- url请求拦截器 -->
8 <servlet>
9 <servlet-name>spring</servlet-name>
10 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
11 </servlet>
12
13 <servlet-mapping>
14 <servlet-name>spring</servlet-name>
15 <url-pattern>/</url-pattern>
16 </servlet-mapping>
17
18 <!-- 字符集过滤器 -->
19 <filter>
20 <filter-name>encodingFilter</filter-name>
21 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
22 <init-param>
23 <param-name>encoding</param-name>
24 <param-value>UTF-8</param-value>
25 </init-param>
26 <init-param>
27 <param-name>forceEncoding</param-name>
28 <param-value>true</param-value>
29 </init-param>
30 </filter>
31 <filter-mapping>
32 <filter-name>encodingFilter</filter-name>
33 <url-pattern>/*</url-pattern>
34 </filter-mapping>
35
36 </web-app>
2. spring-servlet.xml 配置
大家明白spring其实就是一个bean的大容器,类的布局和治本都足以丢给spring,由于这里spring
mvc采取的是‘声明形式’,所以要定义一下‘包‘
环视的限量。那里文件名的定义要清楚一下:<$>-servlet.xml,其中的$就是web.xm中的<servlet-name>spring</servlet-name>的名目spring,最终把
此文件放置在WEB-INF文件夹下,方便tomcat容器加载的时候进行统一读取,详细安顿如下:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
4 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans
6 http://www.springframework.org/schema/beans/spring-beans.xsd
7 http://www.springframework.org/schema/context
8 http://www.springframework.org/schema/context/spring-context.xsd
9 http://www.springframework.org/schema/tx
10 http://www.springframework.org/schema/tx/spring-tx.xsd
11 http://www.springframework.org/schema/mvc
12 http://www.springframework.org/schema/mvc/spring-mvc.xsd">
13
14 <!-- 配置扫描的包 -->
15 <context:component-scan base-package="com.datamip.qncrm.controller"></context:component-scan>
16
17 <!-- 视图解析器 -->
18 <bean
19 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
20 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
21 <property name="prefix" value="/WEB-INF/views/"></property>
22 <property name="suffix" value=".jsp"></property>
23 </bean>
24
25 </beans>
- 新建mvc的views文件夹存放所有view页面
在spring-servlet.xml文件配置节的‘视图解析器’的时候,可以看看有着的jsp页面都要放在views文件夹下,那里本身新建一个index.jsp文件,详细内容如下:
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 <title>首页</title>
8 </head>
9 <body>
10 <h1> SpringMVC 配置成功啦。。。。。</h1>
11 </body>
12 </html>
- 新建mvc的controller控制器
在Java Resources 中的src/main/java
目录下,小编能够新建一个HomeController.java,详细新闻如下:
1 package com.datamip.qncrm.controller;
2
3 import org.springframework.stereotype.Controller;
4 import org.springframework.web.bind.annotation.RequestMapping;
5 import org.springframework.web.bind.annotation.RequestMethod;
6
7 @Controller
8 public class HomeController {
9
10 //路由匹配,使用index.jsp进行页面渲染
11 @RequestMapping(value="/home/index",method=RequestMethod.GET)
12 public String Index() {
13 return "index";
14 }
15 }
好了,基本上spring mvc配置算是截止了,接下去我们在qncrm工程目录上 Run As
-> Run As Server 执行,终于成功啦,也不便于哈~~~,相比较.net 中的一站
式布局,确实难度上涨了过多,然而有一句话说的好,硬是把一个做技术的程序员做成了没技术。倘使大家通晓asp.net
mvc的urlRoutingModule 和MvcHandler,
规律都以如出一辙的,希望本篇对您有帮扶。ZIP文件下载
http://www.bkjia.com/Javabc/1224207.htmlwww.bkjia.comtruehttp://www.bkjia.com/Javabc/1224207.htmlTechArticle改造继续之eclipse集成tomcat开发spring
mvc项目安插一览,tomcatmvc
在上一篇的条件安排中,你还只好依据maven开发一个javase的项目,如果要开发…
在上一篇的条件安排中,你还不得不根据maven开发一个javase的系列,假若要付出一个web项目,还得配置一下tomcat和spring
mvc,集成一览表如下。
一:Tomcat安装
在.net
web开发中,微软再几次向您出示了一站式埃德蒙顿克配置,你只必要轻装一点按钮,发射。。。一个包含bootstrap框架的页面就显将来您的日前,在
java中就不曾这么好的政工了,基本都以高清无码。
- 下载地址
http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.5.20/bin/apache-tomcat-8.5.20.zip,目前tomcat最新版是 9.0
,那里本人采取的8.5本子。
- 陈设环境变量
<1> 在windows平台下,一般会暗许安装在:C:\Program
Files\Apache Software Foundation\汤姆cat 8.5
目录下,在你的windows服务列表中会有一个
Apache 汤姆cat 8.5 汤姆cat8服务项,这样尽管安装到位了。
<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 验证一下,最终检查一下是或不是启动正常。
1 [root@localhost bin]# netstat -tln
2 Active Internet connections (only servers)
3 Proto Recv-Q Send-Q Local Address Foreign Address State
4 tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
5 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
6 tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN
7 tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
8 tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
9 tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
10 tcp6 0 0 :::8009 :::* LISTEN
11 tcp6 0 0 :::111 :::* LISTEN
12 tcp6 0 0 :::8080 :::* LISTEN
13 tcp6 0 0 :::22 :::* LISTEN
14 tcp6 0 0 ::1:631 :::* LISTEN
15 tcp6 0 0 ::1:25 :::* LISTEN
16 tcp6 0 0 127.0.0.1:8005 :::* LISTEN
二: eclipse 和 tomcat 的集成
两者集成起来,绝对如故相比简单的,一般的话做上面三件工作就主旨得以化解了。
- 在eclipse 的 windows -> references
-> server -> enveriment runtime
中先指定你的tomcat版本,比如下图中的
apache tomcat
8.5版本,点击‘next’后指定一下tomcat的设置路径,指定你的jre运行版本即可。
好了,接下去你可以新建一个 maven project ->
选择形式为:’‘maven-archetype-webapp’
,然后填写好集团的域名和项目名称,最终就完结一个
web项目标创制,详细如下图:
- 右击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。
详见步骤看一下题名即可,当一切做完将来,就能消除这么些标题了。
- 添加jstl模板引擎
那些是可选用,如若你创制的 spring
mvc工程运行的时候借使报错说缺少jstl模板,那么在pom.xml引用一下即可。
1 <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
2 <dependency>
3 <groupId>javax.servlet</groupId>
4 <artifactId>jstl</artifactId>
5 <version>1.1.2</version>
6 </dependency>
三:安装spring mvc
到近日截至,tomcat和eclipse的三合一算是告一段落了,接下去要做的就是安装配备spring
mvc。大家可以在maven仓库去拉一下即可,像
spring-core,spring-aop 那些不可或缺看重jar包会全体给您安装收尾。
- web.xml的配置
在src-> main -> webapp -> WEB-INF
下有一个web.xml文件,这几个就相当于.net 中的web.config,在asp.net mvc
中最后是经过mvchandler进行了
请求接管,那种格局在spring
mvc中相同适用,比如:接管的Servlet是DispatcherServlet,web.xml的详实安插如下:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
5 <display-name>Archetype Created Web Application</display-name>
6
7 <!-- url请求拦截器 -->
8 <servlet>
9 <servlet-name>spring</servlet-name>
10 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
11 </servlet>
12
13 <servlet-mapping>
14 <servlet-name>spring</servlet-name>
15 <url-pattern>/</url-pattern>
16 </servlet-mapping>
17
18 <!-- 字符集过滤器 -->
19 <filter>
20 <filter-name>encodingFilter</filter-name>
21 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
22 <init-param>
23 <param-name>encoding</param-name>
24 <param-value>UTF-8</param-value>
25 </init-param>
26 <init-param>
27 <param-name>forceEncoding</param-name>
28 <param-value>true</param-value>
29 </init-param>
30 </filter>
31 <filter-mapping>
32 <filter-name>encodingFilter</filter-name>
33 <url-pattern>/*</url-pattern>
34 </filter-mapping>
35
36 </web-app>
2. spring-servlet.xml 配置
我们领略spring其实就是一个bean的大容器,类的布置和治本都可以丢给spring,由于此地spring
mvc选用的是‘注脚格局’,所以要定义一下‘包‘
举目四望的界定。那里文件名的概念要精通一下:<$>-servlet.xml,其中的$就是web.xm中的<servlet-name>spring</servlet-name>的称谓spring,最终把
此文件放置在WEB-INF文件夹下,方便tomcat容器加载的时候举办统一读取,详细安排如下:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
4 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans
6 http://www.springframework.org/schema/beans/spring-beans.xsd
7 http://www.springframework.org/schema/context
8 http://www.springframework.org/schema/context/spring-context.xsd
9 http://www.springframework.org/schema/tx
10 http://www.springframework.org/schema/tx/spring-tx.xsd
11 http://www.springframework.org/schema/mvc
12 http://www.springframework.org/schema/mvc/spring-mvc.xsd">
13
14 <!-- 配置扫描的包 -->
15 <context:component-scan base-package="com.datamip.qncrm.controller"></context:component-scan>
16
17 <!-- 视图解析器 -->
18 <bean
19 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
20 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
21 <property name="prefix" value="/WEB-INF/views/"></property>
22 <property name="suffix" value=".jsp"></property>
23 </bean>
24
25 </beans>
3.
新建mvc的views文件夹存放所有view页面
在spring-servlet.xml文件配置节的‘视图解析器’的时候,可以见见所有的jsp页面都要放在views文件夹下,那里小编新建一个index.jsp文件,详细内容如下:
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 <title>首页</title>
8 </head>
9 <body>
10 <h1> SpringMVC 配置成功啦。。。。。</h1>
11 </body>
12 </html>
- 新建mvc的controller控制器
在Java Resources 中的src/main/java
目录下,作者得以新建一个HomeController.java,详细消息如下:
1 package com.datamip.qncrm.controller;
2
3 import org.springframework.stereotype.Controller;
4 import org.springframework.web.bind.annotation.RequestMapping;
5 import org.springframework.web.bind.annotation.RequestMethod;
6
7 @Controller
8 public class HomeController {
9
10 //路由匹配,使用index.jsp进行页面渲染
11 @RequestMapping(value="/home/index",method=RequestMethod.GET)
12 public String Index() {
13 return "index";
14 }
15 }
好了,基本上spring mvc配置算是为止了,接下去大家在qncrm工程目录上 Run As
-> Run As Server 执行,终于不负众望啦,也不易于哈~~~,比较.net 中的一站
式布局,确实难度上升了过多,不过有一句话说的好,硬是把一个做技术的程序员做成了没技术。即使大家了然asp.net
mvc的urlRoutingModule 和MvcHandler,
原理都以相同的,希望本篇对你有扶持。ZIP文件下载