新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
HTML使用SpringBoot返回字符串而不是.html文件
要在Spring Boot中返回字符串而不是.html文件,您可以使用@Controller和@ResponseBody注解,以下是一個簡單的示例:

創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、成都網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的灌陽網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
1、創(chuàng)建一個名為StringController的控制器類,并使用@Controller注解標記它。
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StringController {
}
2、在StringController類中添加一個方法,該方法使用@RequestMapping注解映射到特定的URL路徑,并使用@ResponseBody注解返回字符串。
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StringController {
@RequestMapping("/hello")
@ResponseBody
public String hello() {
return "Hello, World!
";
}
}
在這個例子中,當用戶訪問/hello路徑時,將返回一個包含小標題“Hello, World!”的HTML字符串。
如果您想要返回一個單元表格,可以使用以下代碼:
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StringController {
@RequestMapping("/table")
@ResponseBody
public String table() {
return "| Header 1 | Header 2 |
|---|---|
| Row 1, Cell 1 | Row 1, Cell 2 |
| Row 2, Cell 1 | Row 2, Cell 2 |
這將返回一個包含兩列和兩行的簡單表格。
當前題目:HTML使用SpringBoot返回字符串而不是.html文件
當前鏈接:http://www.dlmjj.cn/article/cophpcc.html


咨詢
建站咨詢
