1、创建Maven工程
如果如下图,请执行步骤2
2、添加Tomcat运行环境
右键项目—Build Path—Configure Build Path 选择Tomcat版本,此时工程目录如下
3、切换JRE版本为本级配置的版本
右键项目—Build Path—Configure Build Path 此时工程目录如下
4、编辑web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application
</display-name>
<servlet>
<servlet-name>dispatcher
</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation
</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml
</param-value>
</init-param>
<load-on-startup>1
</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher
</servlet-name>
<url-pattern>/
</url-pattern>
</servlet-mapping>
</web-app>
5、配置dispatcher-servlet.xml
WEB-INF目录下创建文件夹views,用于存放前端开发资源
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-lazy-init="true">
<mvc:annotation-driven />
<context:component-scan base-package="com.rcli.test" />
<mvc:view-controller path="/" view-name="index" />
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
这里配置为POM中的Group Id
6、配置pom.xml文件
<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>
<groupId>com.rcli.test
</groupId>
<artifactId>testwebapp
</artifactId>
<packaging>war
</packaging>
<version>0.0.1-SNAPSHOT
</version>
<name>testwebapp Maven Webapp
</name>
<url>http://maven.apache.org
</url>
<properties>
<project.build.sourceEncoding>UTF-8
</project.build.sourceEncoding>
<springversion>3.1.1.RELEASE
</springversion>
<junitversion>3.8.1
</junitversion>
</properties>
<dependencies>
<dependency>
<groupId>junit
</groupId>
<artifactId>junit
</artifactId>
<version>${junitversion}
</version>
<scope>test
</scope>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-aop
</artifactId>
<version>${springversion}
</version>
<type>jar
</type>
<scope>compile
</scope>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-asm
</artifactId>
<version>${springversion}
</version>
<type>jar
</type>
<scope>compile
</scope>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-aspects
</artifactId>
<version>${springversion}
</version>
<type>jar
</type>
<scope>compile
</scope>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-beans
</artifactId>
<version>${springversion}
</version>
<type>jar
</type>
<scope>compile
</scope>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-context
</artifactId>
<version>${springversion}
</version>
<type>jar
</type>
<scope>compile
</scope>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-context-support
</artifactId>
<version>${springversion}
</version>
<type>jar
</type>
<scope>compile
</scope>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-core
</artifactId>
<version>${springversion}
</version>
<type>jar
</type>
<scope>compile
</scope>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-expression
</artifactId>
<version>${springversion}
</version>
<type>jar
</type>
<scope>compile
</scope>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-jdbc
</artifactId>
<version>${springversion}
</version>
<type>jar
</type>
<scope>compile
</scope>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-jms
</artifactId>
<version>${springversion}
</version>
<type>jar
</type>
<scope>compile
</scope>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-orm
</artifactId>
<version>${springversion}
</version>
<type>jar
</type>
<scope>compile
</scope>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-oxm
</artifactId>
<version>${springversion}
</version>
<type>jar
</type>
<scope>compile
</scope>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-tx
</artifactId>
<version>${springversion}
</version>
<type>jar
</type>
<scope>compile
</scope>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-web
</artifactId>
<version>${springversion}
</version>
<type>jar
</type>
<scope>compile
</scope>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-webmvc
</artifactId>
<version>${springversion}
</version>
<type>jar
</type>
<scope>compile
</scope>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-test
</artifactId>
<version>${springversion}
</version>
<type>jar
</type>
<scope>compile
</scope>
</dependency>
<dependency>
<groupId>javax.servlet
</groupId>
<artifactId>jstl
</artifactId>
<version>1.2
</version>
<type>jar
</type>
<scope>compile
</scope>
</dependency>
<dependency>
<groupId>commons-collections
</groupId>
<artifactId>commons-collections
</artifactId>
<version>3.1
</version>
</dependency>
<dependency>
<groupId>commons-logging
</groupId>
<artifactId>commons-logging
</artifactId>
<version>1.1
</version>
</dependency>
</dependencies>
<build>
<finalName>testwebapp
</finalName>
</build>
</project>
7、src/main/java下新建包com.rcli.test.controller,在包中新建TestController
package com.rcli.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(
"/test")
public class TestController {
@RequestMapping(
"/login")
public String
login() {
return "success";
}
}
上图中 会在之前 配置的路径下找到相关jsp文件 在WEB-INF/webapp/views目录下新建success.jsp,并且把WEB-INF下的index.jsp文件也移到webapp/views目录下
<html>
<body>
<h2>Success!
</h2>
</body>
</html>
8、目录
工程目录如下
运行
1、Tomcat运行
右键工程—Debug As— Debug on Server 有时可能会报如下错误 其实相关包是存在的 最后在stackoverflow上找到了解决方法 http://stackoverflow.com/questions/26257642/servlet-webapp-threw-load-exception 首页 URL框中输入后缀test/login
2、Jetty运行
右键工程—Debug As— Run Jetty
浏览器中输入URL
转载请注明原文地址: https://ju.6miu.com/read-33900.html