mvc项目配置一览
在上豆蔻年华篇的碰到布署中,你还不能不依附maven开辟二个javase的门类,假如要支付二个web项目,还得配置一下tomcat和spring mvc,集成一览表如下。
一:Tomcat安装
在.net web开辟中,微软再贰次向您来得了一条龙杜阿拉克配置,你只须求轻装一点开关,发射。。。一个分包bootstrap框架的页面就呈今后您的先头,在
java中就未有那样好的工作了,基本都是高清无码。
- 下载地址
9.0 ,这里自个儿选拔的8.5版本。
- 铺排景况变量
<1> 在windows平台下,平日会私下认可安装在:C:Program FilesApache Software FoundationTomcat 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 帕特h加多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文件下载