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

时间:2021-05-20

添加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登录页面功能实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章