Use Vue+JFinal framework to build front-end and back-end separation development mode

As a way of web development, the separation of front and back ends is now more and more widely used. The front-end is generally more popular with Vue.js framework, and there are many back-end frameworks. There are many Vue+SpringMVC front-end and back-end separated demos on the Internet, but the Vue+JFinal framework does not seem to be found. This article is based on the Vue.js and JFinal frameworks. A simple example of front-end and back-end separation projects.

The first step: maven builds the back-end JFinal part

1. Create a new web project with maven, the project name is vue-jfinal

2. Add dependencies such as JFinal framework and jetty container

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.jfinal</groupId>
            <artifactId>jfinal</artifactId>
            <version>3.3</version>
        </dependency>
        <dependency>
            <groupId>com.jfinal</groupId>
            <artifactId>jetty-server</artifactId>
            <version>8.1.8</version>
            <scope>provided</scope>
        </dependency>

3. Modify 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>
    <display-name>Archetype Created Web Application</display-name>
    <filter>
        <filter-name>jfinal</filter-name>
        <filter-class>com.jfinal.core.JFinalFilter</filter-class>
        <init-param>
            <param-name>configClass</param-name>
            <param-value>common.demoConfig</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>jfinal</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

4. Add demoController.java to the Package of the new controller in src/main/java

package controller;

import com.jfinal.core.Controller;

public class demoController extends Controller {
    public void index() {
        //跨域请求设置
        getResponse().addHeader("Access-Control-Allow-Origin", "*");
        renderJson("{\"age\":25,\"name\":\"huiliuyi\"}");
    }
}

5. Create a new common Package in src/main/java and add demoConfig.java

package common;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.template.Engine;

import controller.demoController;

public class demoConfig extends JFinalConfig {

    @Override
    public void configConstant(Constants arg0) {
        arg0.setDevMode(true);
    }

    @Override
    public void configEngine(Engine arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void configHandler(Handlers arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void configInterceptor(Interceptors arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void configPlugin(Plugins arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void configRoute(Routes arg0) {
        arg0.add("/demo", demoController.class);
    }

    public static void main(String[] args) {
        JFinal.start("src/main/webapp", 8081, "/", 5);
    }
}

6. Run the demoConfig.java file, run as Java Applicaiton, as shown in the figure below, the backend is completed

In Eclipse Version: Oxygen.1 Release (4.7.1) the project directory is as follows:

Step 2: Use vue-cli to build the vue.js front-end part

1. Install vue-cli

npm install --global vue-cli

2. Create a project based on the webpack template

vue init webpack vue-jfinal

Next, there will be some yes/no options, set as shown below

3. Enter the project directory and install the vue-resource plugin

cd vue- jfinal 
npm install vue-resource

4. Use webstorm to open vue-jfinal, the project directory is as shown below, and set the Run/Debug Configuration

4. Add a reference to vue-resource in main.js

Original file

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})

After adding:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import VueResource from 'vue-resource'
Vue.use(VueResource)

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app' ,
  router,
  components: { App },
  template: '<App/>'
})

5. Modify the App.vue file in the vue-jfinal project

Original file

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <router-view/>
  </div>
</template>

<script>
export default {
  name: 'App'
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

After modification:

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <h1>The server data is: {{serverData}}</h1>
    <button v-on:click="getdata()">Get server-side data</button>
    <router-view/>
  </div>
</template>

<script>
export default {
  name: 'App',
  data(){
    return {
      serverData: "page data"
    }
  },
  methods:{
    getdata(){
      this.$http.get('http://localhost:8081/demo').then(function(response){
        this.serverData=response.data
      })
    }
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

Step 3: Test front-end and back-end separation

1. Click the run button in webstorm, enter http://localhost:8080 in the browser to access, the following interface appears, the front end is successfully built

2. Click the Get back-end data button, and the following figure appears, indicating that the back-end data is obtained successfully

 

Step 4: Put the jfinal project and the vue project in the same project directory, screenshot of the directory

git connection: https://github.com/Feynman61/vue-jfinal.git

Summarize:

The development mode of front-end and back-end separation is very common now, and the advantages are very clear. It separates the work of front-end and back-end developers, so that front-end and back-end developers can focus on their own work and improve development efficiency.

If you have any questions in the actual operation, please leave a comment and I will reply to you as soon as possible.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326875959&siteId=291194637