Gradle学习笔记-搭建javaEE项目

准备工作

  1. 创建工程:选择Gradle工程,指定好目录和项目名即可
  2. build.gradle
plugins {
    id 'java'
    id 'war'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile group: 'org.springframework', name: 'spring-context', version: '5.0.2.RELEASE'
    compile group: 'org.springframework', name: 'spring-web', version: '5.0.2.RELEASE'
    compile group: 'org.springframework', name: 'spring-webmvc', version: '5.0.2.RELEASE'
    providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
    providedCompile group: 'javax.servlet', name: 'jsp-api', version: '2.0'
}

项目编写

仅仅编写一个非常简陋的页面,能够正常访问即可

  1. org.example.controller.HelloController
package org.example.controller;

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

@Controller
@RequestMapping("/hello")
public class HelloController {
    @RequestMapping("/list")
    public String toList(){
        return "list";
    }
}

  1. resources/springmvc.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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--配置组件扫描-->
    <context:component-scan base-package="org.example.controller"/>
    <!--处理器映射器,处理器解析器-->
    <mvc:annotation-driven/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 配置逻辑视图的前缀 -->
        <property name="prefix" value="/WEB-INF/pages/" />
        <!-- 配置逻辑视图的后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>
    <!--释放静态资源-->
    <mvc:default-servlet-handler/>
</beans>
  1. webapp下需要编写三个文件
  • webapp/WEB-INF/web.xml
<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app 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"
         version="3.0">
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc.xml</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>


  • webapp/index.jsp
<%--
  Created by IntelliJ IDEA.
  User: xiejj01
  Date: 2020/11/20
  Time: 10:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <jsp:forward page="hello/list"/>
</body>
</html>

  • webapp/WEB-INF/pages/list.jsp
<%--
  Created by IntelliJ IDEA.
  User: xiejj01
  Date: 2020/11/20
  Time: 10:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    success
</body>
</html>

Tomcat发布

  1. 添加tomcat
    在这里插入图片描述
  2. 选择war包
    在这里插入图片描述
    3.启动后成功访问
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44112790/article/details/109840427