Skip to content

Commit

Permalink
✅测试前后端分离
Browse files Browse the repository at this point in the history
  • Loading branch information
mewhz committed Dec 30, 2021
1 parent 893cd89 commit 8a6d60a
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 23 deletions.
36 changes: 34 additions & 2 deletions src/main/java/com/mewhz/paste/controller/CodeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@


import cn.hutool.db.Entity;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.mewhz.paste.utils.CodeSQL;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* @author mewhz
*/
@Controller
@RestController
public class CodeController {

@RequestMapping("/code")
@ResponseBody
public String code(@RequestParam String id) {
String htmlFront = "<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
Expand Down Expand Up @@ -83,4 +85,34 @@ else if ("python".equals(type)){

return html.toString();
}


@RequestMapping("/codes")
public JSONObject codes(@RequestParam String id){

JSONObject json = JSONUtil.createObj();
String type = null;
String text = null;
String remark = null;

CodeSQL codesql = new CodeSQL();
List<Entity> list = codesql.findCode(id);

type = (String) list.get(0).get("type");
text = (String) list.get(0).get("text");
remark = (String) list.get(0).get("remark");

// text = text.replaceAll("<", "&lt;");
// text = text.replaceAll(">", "&gt;");
// text = text.replaceAll("\n", "<br>");
// text = text.replaceAll(" ", "&nbsp;");


json.append("text", text);
json.append("type", type);
json.append("remark", remark);


return json;
}
}
30 changes: 15 additions & 15 deletions src/main/resources/static/code.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@
</head>
<body>
<pre>
<code class="language-cpp" id="code">#include &lt;iostream&gt;
using namespace std;
int main() {
int n;
cin >> n;
cout << n << endl;
return 0;
<code class="language-java" id="code">
package com.mewhz.paste;

public class Main{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
</code>
</pre>
<button id="button" class="btn btn-success" onclick="copy()" data-clipboard-target="#code" data-clipboard-action="copy">一键复制</button>
<!-- <button id="button" class="btn btn-success" onclick="copy()" data-clipboard-target="#code" data-clipboard-action="copy">一键复制</button>-->
<script src="js/prism.js"></script>
<script src="js/prism-c.min.js"></script>
<script src="js/prism-cpp.min.js"></script>
<script src="js/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/clipboard.min.js"></script>
<script src="js/code.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
<!-- <script src="js/prism-java.min.js"></script>-->
<!-- <script src="js/prism-c.min.js"></script>-->
<!-- <script src="js/prism-cpp.min.js"></script>-->
<!--<script src="js/jquery-3.6.0.min.js"></script>-->
<!-- <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/clipboard.min.js"></script>-->
<!--<script src="js/code.js"></script>-->
<!--<script src="https://cdn.bootcdn.net/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>-->
<script src="js/prism-java.min.js"></script>
<!-- <script src="js/prism-python.min.js"></script>-->
</body>
</html>
25 changes: 25 additions & 0 deletions src/main/resources/static/codes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="css/prism.css" rel="stylesheet">
<link rel="stylesheet" href="css/bootstrap.min.css"/>
<link href="https://cdn.bootcdn.net/ajax/libs/toastr.js/latest/css/toastr.min.css" rel="stylesheet">
</head>
<body>
<pre>
<code class="language-java" id="code">
package com.mewhz.paste;

public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
</code>
</pre>
<script src="js/prism.js"></script>
<script src="js/prism-java.min.js"></script>
</body>
</html>
18 changes: 16 additions & 2 deletions src/main/resources/static/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,25 @@
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="css/prism.css" rel="stylesheet">
<link rel="stylesheet" href="css/bootstrap.min.css"/>
<link href="https://cdn.bootcdn.net/ajax/libs/toastr.js/latest/css/toastr.min.css" rel="stylesheet">
<script src="js/jquery-3.6.0.min.js"></script>
<script src="js/head.js"></script>
</head>
<body>
<h1></h1>
<pre>
<code id="code">

</code>
</pre>
<button id="button" class="btn btn-success" onclick="copy()" data-clipboard-target="#code" data-clipboard-action="copy">
一键复制
</button>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/clipboard.min.js"></script>
<script src="js/code.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
<script src="js/prism.js"></script>
<script src="js/prism-java.min.js"></script>
</body>
</html>
</html>
12 changes: 8 additions & 4 deletions src/main/resources/static/js/head.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@


$(document).ready(()=>{
$.getJSON("/head", (result)=>{
$.getJSON("/codes?id=1637767613075", (result)=>{
console.log(result);
$.each(result, (i, temp)=>{
$("h1").append(temp + " ");
});
console.log(result.type);
$("#code").attr("class","language-java");
$("#code").text(result.text);
$("title").text(result.remark);
Prism.highlightAll();
})
});
7 changes: 7 additions & 0 deletions src/test/java/com/mewhz/paste/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mewhz.paste;

public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

0 comments on commit 8a6d60a

Please sign in to comment.