spring boot jsp页面 SpringBoot创建JSP登录页面功能实例代码
C4PO 人气:0想了解SpringBoot创建JSP登录页面功能实例代码的相关内容吗,C4PO在本文为您仔细讲解spring boot jsp页面的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:spring,boot,jsp页面,spring,boot,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登录页面功能实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
加载全部内容