新聞中心
要在Spring Boot中返回字符串而不是.html文件,您可以使用@Controller和@ResponseBody注解,以下是一個(gè)簡單的示例:

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


咨詢
建站咨詢
