Tomcat运行:
首先先创建一个springmvc项目
配置组织名和项目名
配置maven
配置项目路径
pom.xml文件代码:
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--打包war包-->
<packaging>war</packaging>
<name>demotest2</name>
<groupId>com.accp</groupId>
<artifactId>demotest2</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--springmvc包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
</dependencies>
</project>
web.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--springmvc过滤器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
springmvc-servlet.xml代码:
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx">
<!--视图解析-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--扫描-->
<context:component-scan base-package="com.accp.controller"/>
</beans>
写一个控制器测试:
package com.accp.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@RequestMapping("show")
public String show(){
return "index";
}
}
在WEB-INF下创建jsp文件夹,里面创建一个jsp页面测试:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
滴滴答答滴滴答答
</body>
</html>
然后配置tomcat
然后启动项目是否正常:
停止项目,点击Maven Projects,按图操作
然后可以看出war包已生成成功
将war包放到tomcat的webapps下,然后启动tomcat
然后浏览器访问:
Linux虚拟机运行:
可以使用第三方工具Xftp 6将装有war包的tomcat拷到虚拟机上,方便快捷
使用Xshell 6第三方工具进行测试,首先输入命令 ls 查看当前文件
进入tomcat文件:
查看当前文件,然后进入bin文件:
查看当前文件夹中的文件:
使用命令./运行catalina.sh文件,如果权限不够,在文件前加chmod 777 授权
输入命令,tomcat启动:
打开浏览器访问: