您好,登錄后才能下訂單哦!
這篇文章給大家介紹利用SpringBoot創建一個JSP登錄頁面,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
添加JSP配置
1.pom.xml添加jsp解析引擎
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <scope>provided</scope> </dependency>
2.在項目com.xxxx下創建文件夾config,在com.xxx.config下添加spring-boot配置JspConfig.java
public class JspConfig extends WebMvcConfigurerAdapter{ @Bean public InternalResourceViewResolver internalResourceViewResolver () { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); return viewResolver; } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } }
3.創建/src/main/webapp/WEB-INF/views
目錄結構
src --- main --- java | | --- basic | | --- config | | --- controller | | | --- service | | --- persistent | | --- resources | | --- webapp --- WEB-INF - views
login.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <h2>Login Page</h2> <!--表單提交目標鏈接和方法post--> <form action="/account/valid" method="post"> <input type="text" name="name" placeholder="用戶名"> <input type="password" name="password" placeholder="密碼"> <input type="submit" value="登錄"> </form> </html>
AccountController.java
@Controller @RequestMapping("/account") //RequestMapping可對類和方法做映射 public class AccountController { @Autowired //自動封裝注解,省去set、get方法和實例化聲明 AccountService accountService; @RequestMapping(value="/login") public String Login() { return "login"; //沒有用ResponseBody,返回string,跳轉的是jsp頁面:"login" + ".jsp"后綴 } @RequestMapping("/valid") @ResponseBody public String Valid(HttpServletRequest request) { Account account = new Account(); account.setName(request.getParameter("name")); account.setPassword(request.getParameter("password")); if(accountService.valid(account)) { return "Login Success!"; } else { return "Login Failed!"; } } }
AccountService.java
@Service public class AccountService { public boolean valid (Account account) { if("admin".equals(account.getName()) && "123456".equals(account.getPassword())) { return true; } return false; } }
關于利用SpringBoot創建一個JSP登錄頁面就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。