程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> SpringBoot創建JSP登錄頁面功能實例代碼

SpringBoot創建JSP登錄頁面功能實例代碼

編輯:關於JAVA

SpringBoot創建JSP登錄頁面功能實例代碼。本站提示廣大學習愛好者:(SpringBoot創建JSP登錄頁面功能實例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是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>
  <h1>Login Page</h1>
  <!--表單提交目標鏈接和方法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登錄頁面功能實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對網站的支持!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved