Skip to content

Commit

Permalink
Merge pull request #31 from terwer/j2v8-version
Browse files Browse the repository at this point in the history
j2v8 no router version run success
  • Loading branch information
terwer committed Feb 14, 2019
2 parents f303ee8 + 6397e54 commit 3fff2fe
Show file tree
Hide file tree
Showing 21 changed files with 1,174 additions and 297 deletions.
23 changes: 14 additions & 9 deletions README.md
Expand Up @@ -29,31 +29,36 @@ With Vue,webpack,Spring Boot and eclipse j2v8 Script engine for server-side-rend
1、Go to [webapp](src/main/webapp) and run vue ssr build

```bash
cd src/main/webapp && yarn && yarn build-ssr
cd src/main/webapp && yarn && yarn build
```

# compile && run test
notice:You can run ``yarn dev`` in development mode to show vue error logs

2、Run java maven build

```bash
mvn -v && mvn compile && mvn exec:java
mvn clean package -DskipTests
```

notice:You can run ``yarn build-ssr-dev`` in development mode to show error logs

2、Run java mavem build
# compile && run cli

```bash
mvn clean package -DskipTests
mvn -v && mvn compile && mvn exec:java
```

3、Copy ``target/ROOT.war`` to ``${TOMCAT_HOME}/webapps``
# run && deploy

or
## run

```bash
mvn clean spring-boot:run
```

## deploy

Copy ``target/ROOT.war`` to ``${TOMCAT_HOME}/webapps``


# Structure

The whole project is a ``Java Spring Boot Maven`` structure,the ``src/main/webapp`` is a complete ``Vue`` Project With ``webpack`` structure
Expand Down
12 changes: 3 additions & 9 deletions pom.xml
Expand Up @@ -12,15 +12,15 @@
<artifactId>jvue</artifactId>
<version>${projectVersion}</version>
<name>${projectName}</name>
<description>Next light-weight,responsive project With Vue,Spring Boot and Java GraalVM.js Script engine for server-side-rendering</description>
<description>Next light-weight,responsive project With Vue,webpack,Spring Boot and eclipse j2v8 Script engine for server-side-rendering</description>

<profiles>
<profile>
<id>application-config</id>
<properties>
<activatedProperties>dev</activatedProperties>
<projectName>JVue Framework</projectName>
<projectVersion>1.0.2</projectVersion>
<projectVersion>1.0.3</projectVersion>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
Expand Down Expand Up @@ -191,15 +191,9 @@
<directory>src/main/webapp</directory>
<filtering>false</filtering>
<includes>
<!-- 客户端渲染
<include>dist/**</include>
-->
<!-- 服务端渲染 -->
<include>templates/**</include>
<include>dist/**</include>
</includes>
<excludes>
<exclude>templates/entry-server.css</exclude>
</excludes>
</resource>
</resources>

Expand Down
24 changes: 21 additions & 3 deletions src/main/java/com/terwergreen/jvue/CLI.java
@@ -1,5 +1,8 @@
package com.terwergreen.jvue;

import com.alibaba.fastjson.JSON;
import com.terwergreen.jvue.vendor.j2v8.V8Context;
import com.terwergreen.jvue.vendor.j2v8.impl.V8ContextImpl;
import com.terwergreen.jvue.vendor.vue.VueRenderer;
import com.terwergreen.jvue.vendor.vue.impl.VueRendererImpl;
import org.apache.commons.logging.Log;
Expand All @@ -11,13 +14,28 @@
public class CLI {
private static final Log logger = LogFactory.getLog(CLI.class);

/**
* 运行命令
* mvn -v && mvn compile && mvn exec:java
*
* @param args 参数
*/
public static void main(String[] args) {
// 设置路由上下文
Map<String, Object> httpContext = new HashMap<>();
httpContext.put("url", "/about");
httpContext.put("url", "/");

// 添加seo
httpContext.put("title", "title");
Map<String, Object> metaMap = new HashMap<>();
metaMap.put("keywords", "keywords");
metaMap.put("description", "description");
httpContext.put("meta", metaMap);

// 渲染Vue
VueRenderer vueRenderer = new VueRendererImpl();
vueRenderer.renderContent(httpContext);
V8Context v8Context = new V8ContextImpl();
VueRenderer vueRenderer = new VueRendererImpl(v8Context);
Map<String, Object> resultMap = vueRenderer.renderContentCLI(httpContext);
logger.info("resultMap=>" + JSON.toJSONString(resultMap));
}
}
5 changes: 2 additions & 3 deletions src/main/java/com/terwergreen/jvue/config/WebConfig.java
Expand Up @@ -29,9 +29,8 @@ public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
logger.info("添加静态资源映射");
// 静态资源映射
// registry.addResourceHandler("/**").addResourceLocations("classpath:/dist/");
registry.addResourceHandler("/**").addResourceLocations("classpath:/templates/");
// Vue静态资源映射
registry.addResourceHandler("/**").addResourceLocations("classpath:/dist/");

// swagger-ui
logger.info("映射swagger-ui");
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/com/terwergreen/jvue/controller/MainController.java
@@ -1,10 +1,14 @@
package com.terwergreen.jvue.controller;

import com.terwergreen.jvue.util.VueUtil;
import com.terwergreen.jvue.vendor.vue.VueRenderer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -18,18 +22,28 @@
**/
@Controller
public class MainController {
private final Log logger = LogFactory.getLog(this.getClass());

@Autowired
private VueRenderer vueRenderer;

@RequestMapping("/")
@RequestMapping(value = "/", produces = "text/html;charset=UTF-8")
@ResponseBody
public String index(Model model) {
// 设置路由上下文
Map<String, Object> context = new HashMap<>();
context.put("url", "/");
Map<String, Object> httpContext = new HashMap<>();
httpContext.put("url", "/");

// 添加seo
httpContext.put("title", "title");
Map<String, Object> metaMap = new HashMap<>();
metaMap.put("keywords", "keywords");
metaMap.put("description", "description");
httpContext.put("meta", metaMap);

Map<String, Object> resultMap = vueRenderer.renderContent(context);
model.addAllAttributes(resultMap);
return "index.ssr";
// 返回服务端渲染后的结果
Map<String, Object> resultMap = vueRenderer.renderContent(httpContext);
return VueUtil.resultMapToString(resultMap);
}

@RequestMapping("/home")
Expand Down
@@ -1,12 +1,7 @@
package com.terwergreen.jvue.servlet;

import com.eclipsesource.v8.NodeJS;
import com.eclipsesource.v8.V8;
import com.terwergreen.jvue.vendor.j2v8.V8Context;
import com.terwergreen.jvue.vendor.j2v8.impl.V8ContextImpl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.web.servlet.DispatcherServlet;

Expand All @@ -17,38 +12,19 @@
import java.io.IOException;

/**
* @Author Terwer
* @Date 2018/7/25 14:44
* @Version 1.0
* @Description 自定义DispatcherServlet
**/

/**
* 自定义DispatcherServlet
* Spring Boot为了添加你自己的servlet,需声明一个Servlet类型的@Bean,并给它特定的bean名称dispatcherServlet(如果只想关闭但不替换它,你可以使用该名称创建不同类型的bean)。
*
* @author Terwer
* @version 1.0
* 2018/7/25 14:44
*/
public class JVueDispatcherServlet extends DispatcherServlet implements V8Context {
public class JVueDispatcherServlet extends DispatcherServlet {
private Logger logger = LogManager.getLogger(this.getClass());

private V8Context v8Context;

@Override
public V8 getV8() {
V8 v8= v8Context.getV8();
v8.getLocker().acquire();
return v8;
}

@Override
public NodeJS getNodeJS() {
return v8Context.getNodeJS();
}

@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
// 在这里初始化v8和nodejs
logger.info("servlet初始化完毕,开始初始化v8和nodejs...");
v8Context = new V8ContextImpl();
}

@Override
Expand Down
52 changes: 48 additions & 4 deletions src/main/java/com/terwergreen/jvue/util/VueUtil.java
Expand Up @@ -16,8 +16,10 @@
import java.io.Reader;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
Expand All @@ -32,7 +34,7 @@ public class VueUtil {
/**
* Vue资源文件目录
*/
private static final String VUE_RESOURCE_PATH = "/templates/";
private static final String VUE_RESOURCE_PATH = "/dist/";

/**
* 读取脚本
Expand Down Expand Up @@ -82,21 +84,34 @@ public static File readVueFile(final String fileName) {
return file;
}

private static String getVueFileResource(final String fileName) {
try {
// 获取JS路径
URL resourcePath = VueUtil.class.getResource(VUE_RESOURCE_PATH + fileName);
String fullResourcePath = Paths.get(resourcePath.toURI()).toFile().getAbsolutePath();
logger.info("fullResourcePath = " + fullResourcePath);
return fullResourcePath;
} catch (URISyntaxException e) {
logger.error("Vue文件路径错误", e);
}
return null;
}

/**
* 根据正则名称获取Vue资源文件
*
* @param fileName 文件名称
* @param fileNameRegex 文件名称正则
* @return 匹配的第一个文件
*/
private static String getVueFileResource(final String fileName) {
private static String getVueFileResourceRegex(final String fileNameRegex) {
List<String> filenameList = new ArrayList<>();
try {
// 获取JS路径
URL resourcePath = VueUtil.class.getResource(VUE_RESOURCE_PATH);
logger.info("resourcePath = " + resourcePath.toURI());
File path = new File(resourcePath.toURI());
String[] list = path.list(new FilenameFilter() {
private Pattern pattern = Pattern.compile(fileName);
private Pattern pattern = Pattern.compile(fileNameRegex);

@Override
public boolean accept(File dir, String name) {
Expand All @@ -120,4 +135,33 @@ public boolean accept(File dir, String name) {
}
return null;
}

/**
* 结果Map转换
*
* @param resultMap 结果Map
* @return 结果html
*/
public static String resultMapToString(Map<String, Object> resultMap) {
Integer renderStatus = (Integer) resultMap.getOrDefault("renderStatus", 0);
StringBuilder sb = new StringBuilder();
String content = (String) resultMap.getOrDefault("content", "");
sb.append(content);

if (renderStatus == 1) {
logger.info("服务端渲染成功");
}
Integer isShowError = (Integer) resultMap.getOrDefault("isShowError", 0);
if (isShowError == 1) {
String data = (String) resultMap.getOrDefault("data", "");
sb.append(data);
}
return sb.toString();
}

public static void main(String[] args) {
String fileName = "lib/he.js";
String appFilename = getVueFileResource(fileName);
System.out.println("appFilename = " + appFilename);
}
}
11 changes: 9 additions & 2 deletions src/main/java/com/terwergreen/jvue/vendor/vue/VueRenderer.java
Expand Up @@ -8,8 +8,15 @@
public interface VueRenderer {
/**
* 根据上下文渲染实例
* @param context 上下文
* @param httpContext 上下文
* @return 服务端html及对应状态
*/
Map<String,Object> renderContent(Map<String, Object> context);
Map<String,Object> renderContentCLI(Map<String, Object> httpContext);

/**
* 根据上下文渲染实例
* @param httpContext 上下文
* @return 服务端html及对应状态
*/
Map<String,Object> renderContent(Map<String, Object> httpContext);
}

0 comments on commit 3fff2fe

Please sign in to comment.