Gradle study notes-split JavaEE project

Ready to work

create

First create the parent project normally,
Insert picture description here
and then create the sub-modules: right-click the parent project->new->module
Insert picture description here
I created three sub-modules as a demonstration here
Insert picture description here

Configuration

Common configuration of all modules

allprojects {
    plugins {
        id 'java'
    }

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

    repositories {
        mavenLocal()
        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'
    }
}

gradle_dao

  1. Since I don’t use a database, it’s just a simple demonstration and no other configuration is needed.
    Insert picture description here

  2. The code has only one interface and one implementation class

  • interface
package org.example.dao;
public interface UserDao {
    public String save();
}

  • Implementation class
package org.example.dao.impl;

import org.example.dao.UserDao;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao {
    @Override
    public String save() {
        return "save success";
    }
}

gradle_service

  1. Rely on dao to provide services, need to be added in build.gradle
dependencies {
    compile project(":gradle_dao")
}
  1. The code only wrote the interface and implementation class
  • interface
package org.example.service;

public interface UserService {
    public String save();
}

  • Implementation class
package org.example.service.impl;

import org.example.dao.UserDao;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public String save() {
        return userDao.save();
    }
}

gradle_view

  1. War package plug-in is required, and there are some more jsp dependencies

The dependency of providedCompile will take effect after enabling the war package plugin

apply plugin: 'war'


dependencies {
    compile project(":gradle_service")
    providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
    providedCompile group: 'javax.servlet', name: 'jsp-api', version: '2.0'
}
  1. The code only writes a controller
package org.example.controller;

import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/save")
    public String save(Model model){
        String save = userService.save();
        model.addAttribute("save", save);
        return "list";
    }
}

  1. Add springmvc.xml under resources
<?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"/>
    <!--处理器映射器,处理器解析器-->
    <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. There are WEB-INF/web.xml, index.jsp, WEB-INF/pages/ in webapp
  • 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>


  • index.jsp
<%--
  Created by IntelliJ IDEA.
  User: jiajunbernoulli
  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="user/save"/>
</body>
</html>

  • pages
    only wrote a list.jsp file
<%--
  Created by IntelliJ IDEA.
  User: jiajunbernoulli
  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>
    ${save} 跳转到列表页面
</body>
</html>

Post a visit

Finally, set tomcat,
Insert picture description here
don’t forget to select the war package
Insert picture description here
and visit after startup
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44112790/article/details/109923403