在spring boot中,任何URL都沒有問題,比如localhost:8080/anything,但只有在默認URL中,比如localhost:8080/或者localhost:8080。
IDE 當前使用 Intellij。
這沒有被完成。我試圖抓住一個斷點來嘗試除錯,但我一開始沒有運行那個方法
除錯時確認了bean是否生成,但是確認生成bean沒有問題。
這沒有被完成。我試圖抓住一個斷點來嘗試除錯,但我一開始沒有運行那個方法
除錯時確認了bean是否生成,但是確認生成bean沒有問題。
@Slf4j
@Controller
public class HomeController {
@RequestMapping("/")
public String index(){
return "index";
}
}
它作業正常。無論設定什么 URI,它都可以正常作業。
@Slf4j
@Controller
public class HomeController {
@RequestMapping("/anyting")
public String index(){
return "index";
}
}
我的醬汁如下 我應該檢查什么?
預先感謝您的幫助。
uj5u.com熱心網友回復:
嘗試給它它的作業原理:
@Slf4j
@Controller
public class HomeController {
@GetMapping("")
public String index(){
return "index";
}
}
uj5u.com熱心網友回復:
您可以嘗試以下方法:
1) 將@Controller 與@ResponseBody 一起使用
@Controller用于將類標記為 Spring MVC 控制器。如果僅使用@Controller,則必須@ResponseBody在每個 API 方法中指定。
@Controller
@Slf4j
public class HomeController {
@RequestMapping("/")
@ResponseBody
public String index() {
return "index";
}
@RequestMapping("/anything")
@ResponseBody
public String index2() {
return "index";
}
}
輸出:以上代碼將回傳“index”字串作為此 API 的回應。
如果你想顯示 index.html 頁面,那么你必須修改你的代碼如下,請注意你的 index.html 應該放在 resources/static 檔案夾中。
@RequestMapping("/")
@ResponseBody
public ModelAndView index() {
return new ModelAndView("index.html");
}
輸出:這會將您重定向到 index.html 頁面。
2)使用@RestController
@RestControllerannotation 是 RESTful Web 服務中使用的特殊控制器,它是 @Controller 和 @ResponseBody annotation 的組合。它是@Component 注解的特殊版本。
@RestController
@Slf4j
public class HomeController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/anything")
public String index2() {
return "index";
}
}
輸出:以上代碼將回傳“index”字串作為此 API 的回應。
如果你想顯示 index.html 頁面,那么你必須修改你的代碼如下,請注意你的 index.html 應該放在 resources/static 檔案夾中。
@RequestMapping("/")
public ModelAndView index() {
return new ModelAndView("index.html");
}
輸出:這會將您重定向到 index.html 頁面。
uj5u.com熱心網友回復:
每個人。謝謝你回答我的問題。
但它沒有解決我。
相反,我們將以另一種方式解決它并與您分享。
@xerx593 的第一個詞,告訴我將日志記錄級別設定為除錯,我得到了提示。
2022-10-13 16:54:01.710 DEBUG 79836 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : GET "/", parameters={}
2022-10-13 16:54:01.712 DEBUG 79836 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ParameterizableViewController [view="forward:/index"]
2022-10-13 16:54:01.724 DEBUG 79836 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8]
2022-10-13 16:54:01.724 DEBUG 79836 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView : View name 'forward:', model {}
2022-10-13 16:54:01.725 DEBUG 79836 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView : Forwarding to [/index]
2022-10-13 16:54:01.728 DEBUG 79836 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : "FORWARD" dispatch for GET "/index", parameters={}
2022-10-13 16:54:01.734 DEBUG 79836 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [static/], classpath [public/], classpath [], classpath [resources/], classpath [META-INF/resources/], classpath [META-INF/resources/webjars/]]
2022-10-13 16:54:01.736 DEBUG 79836 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2022-10-13 16:54:01.736 DEBUG 79836 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Exiting from "FORWARD" dispatch, status 404
2022-10-13 16:54:01.740 DEBUG 79836 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
(將日志匯入片段以供查看。)
如果您檢查日志并接近“/”,則將其轉發到“/index”。
所以我改變了代碼來接收“/index”并處理它。
@Slf4j
@Controller
public class HomeController {
@RequestMapping("/index")
public String index(){
return "index";
}
}
這個對我有用。
謝謝你。
uj5u.com熱心網友回復:
問題:
不確定/無法重現!
實作:templates/index.html從請求服務到<context_root>/“默認
當 Controller做一些額外的 BL時
我可以像這樣作業:
@Controller ... class ... { // @RestController returns rather a "response body" than the "view name".
@RequestMapping("/"/*, add more here!?*/) // !
public String index(/*...*/){
// do something useful..., then:
return "index";
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/515246.html
