服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - SpringBoot创建JSP登录页面功能实例代码

SpringBoot创建JSP登录页面功能实例代码

2020-09-05 11:48C4PO Java教程

这篇文章主要介绍了SpringBoot创建JSP登录页面功能实例代码,非常不错,具有参考借鉴价值,需要的朋友可以参考下

添加JSP配置

1.pom.xml添加jsp解析引擎

?
1
2
3
4
5
6
7
8
9
10
<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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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

目录结构

?
1
2
3
4
5
6
7
8
9
10
src --- main --- java
     |    | --- basic
     |    | --- config
     |    | --- controller       |
     |    | --- service
     |    | --- persistent
     |
     | --- resources
     |
     | --- webapp --- WEB-INF - views

login.jsp

?
1
2
3
4
5
6
7
8
9
10
11
<%@ 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@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

?
1
2
3
4
5
6
7
8
9
@Service
public class AccountService {
  public boolean valid (Account account) {
    if("admin".equals(account.getName()) && "123456".equals(account.getPassword())) {
      return true;
    }
    return false;
  }
}

以上所述是小编给大家介绍的SpringBoot创建JSP登录页面功能实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://www.jianshu.com/p/2d08f3e64fe3

延伸 · 阅读

精彩推荐