首页 > 开发 > Java > 正文

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

2024-07-13 10:06:01
字体:
来源:转载
供稿:网友

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

@Servicepublic class AccountService {  public boolean valid (Account account) {    if("admin".equals(account.getName()) && "123456".equals(account.getPassword())) {      return true;    }    return false;  }}

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


注:相关教程知识阅读请移步到JAVA教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表