服务器之家

服务器之家 > 正文

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

时间:2020-09-05 11:48     来源/作者:C4PO

添加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

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
返回顶部