Eclipse Tomcat & Maven 的配置,创建简单 SpringMVC 项目

chan 作者
阅读 330 喜欢 0

公司不能使用 idea ,因此要重新适应 eclipse ,进行开发,以下做一个简单配置的笔记

安装 Eclipse

安装的版本是 Eclipse IDE 2018-12 跟公司保持版本一致。

官方网站:https://www.eclipse.org/downloads/packages/release/2018-12/r

Eclipse 实用配置

  • 类名自动补全

window -> java -> editor -> content assist

查看 Auto Activation 编辑栏下的 Auto activation triggers for java:

将原本的 . 改为 .qwertyuiopasdfghjklzxcvbnm 即可

  • 折叠包路径

  • 去掉 java resource 路径

常用快捷键

  • 补全 new 对象:alt + shift + L

  • 代码格式化:crtl + shift + F

配置 Maven

  • 安装插件:Help -> Install New Software -> Add

m2e
http://download.eclipse.org/technology/m2e/releases

  • Maven 地址:windows -> preferences -> maen -> installations

选择 Maven 安装路径

  • 选择配置文件:Maven -> User Settings

可以通过 settings.xml 选定对应的仓库地址

配置 Tomcat

  • windows -> preferences -> server -> runtime environments

添加服务器

使用 Maven 创建 SpringMVC 项目

  • file -> new -> other -> maven project

  • 选择 maven-archetype-webapp

  • 编辑 pom 文件添加依赖
<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.test</groupId>
  <artifactId>spring_mvc</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>spring_mvc Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
      <spring.version>4.1.1.RELEASE</spring.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>spring_mvc</finalName>
  </build>
</project>
  • 右键项目 -> maven -> update project

勾选项目,OK 引入依赖的 jar 文件

导入完成如下:

  • 由于 SpringMVC 需要从 WEB-INF -> lib 下读取包,因此还需要配置:右键项目 -> properties -> Deployment Assembly -> add

选择 java build path entries -> next -> 选中 Maven Dependencies

效果如下:

web.xml

<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>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-context.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>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

spring-content.xml

Resource 包下创建 spring-content.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.test" />

    <!-- 视图页面配置 -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>

JSP 视图

分别创建两个视图:
web-app -> WEB-INF -> view -> view.jsp
web-app -> index.jsp

Controller

创建控制层

package com.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Controller
public class HelloSpringController {

    @RequestMapping("/view")
    public String showMessage() {
        return "view";
    }

}

使用 Tomcat 服务器运行项目

  • 右键项目 -> run as -> run on server

  • 项目根路径

  • Veiw 接口 (控制层)

全部评论0