首先不使用模板
那該如何做呢
先建一個(gè)controller如下
@Controller
public class indexController {
@RequestMapping("/user")
public ModelAndView index() {
return new ModelAndView("login");
}
}
注意:@controller很重要,不要使用@restcontroller百新,因?yàn)樗欠祷豭son
嗯昭抒,在resource/static下新建一個(gè)login.html
<body>
<form action="/user/login" method="post">
請(qǐng)輸入用戶名 <input type="text" name="name" style="align-content: center"/>
<br>
請(qǐng)輸入密碼 <input type="password" name="password" style="align-content: center"/>
<br>
<input type="submit" value="Submit" style="margin-left: 50px"/>
<br>
<a href="register.html">未注冊(cè)的話危彩,請(qǐng)點(diǎn)擊該行文字</a>
</form>
</body>
然后再新建一個(gè)controller
@Controller
@RequestMapping("/user")
public class LoginController {
@Autowired
private ILoginService iLoginService;
@PostMapping(value = "/login")
public ModelAndView postUser(HttpServletRequest request) {
String name=request.getParameter("name");
String password=request.getParameter("password");
String text = iLoginService.loginUser(name, password);
if (text.equals("登錄成功")) {
return new ModelAndView("userPage");
} else {
return new ModelAndView("redirect:/user");
}
}
}
從一般接口的請(qǐng)求方式到HttpServletRequest request徒欣,然后能出現(xiàn)表單等按鈕逐样,點(diǎn)擊之后
卻死活顯示不出來(lái)
再數(shù)據(jù)庫(kù)中一查,數(shù)據(jù)有變動(dòng),說(shuō)明邏輯代碼是對(duì)的脂新,那為什么就不行呢
百度了好久挪捕,,争便,级零,,始花,妄讯,好久孩锡,酷宵,,躬窜,浇垦,好久,荣挨,男韧,
竟然發(fā)現(xiàn)也有人和我遇到一樣的問(wèn)題,哈默垄,原來(lái)還有同道中人哇
唧唧此虑,但是他們也沒(méi)得出結(jié)論
百度一般的結(jié)果就是:不要@restcontroller啥的
終于百度到了一個(gè)可以有用的,哈口锭,就是使用模板
再pom下添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
再application.properties下添加配置
spring.thymeleaf.prefix=classpath:/templates/
然后奇跡出現(xiàn)啦朦前,竟然成功了
能跳到userPage的網(wǎng)頁(yè)