JavaWeb 开发02 web实验

Thymeleaf现代的Java模板引擎

Thymeleaf is a modern server-side Java template engine for both web
and standalone environments.

Thymeleaf’s main goal is to bring elegant natural templates to your
development workflow — HTML that can be correctly displayed in
browsers and also work as static prototypes, allowing for stronger
collaboration in development teams.

With modules for Spring Framework, a host of integrations with your
favourite tools, and the ability to plug in your own functionality,
Thymeleaf is ideal for modern-day HTML5 JVM web development — although
there is much more it can do.

Thymeleaf使用:
引入Starter:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

自动配置的策略

  1. 所有thymeleaf的配置值都在 ThymeleafProperties
  2. 配置好了 SpringTemplateEngine
  3. 配好了 ThymeleafViewResolver
  4. 我们只需要直接开发页面
    在这里插入图片描述

1.创建一个新的项目

2.配置资源

首先下载admin压缩文件(密码:2xc3)并解压:

将文件夹中的资源都放入resources的static包中:

在这里插入图片描述

将页面放入templates中:

在这里插入图片描述

3.在controller包中新建一个IndexController.java作为登录页面的控制层:

在这里插入图片描述

package com.it.springbootoracle.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class IndexController {
    /**
     * 来登录页 设置当进入localhost时跳转到登录界面
     * @return
     */
    @GetMapping(value = {"/","/login"})
    public String loginPage(){

        return "login";
    }

//    @PostMapping("/login")
//    public String main(User user, HttpSession session, Model model){ //RedirectAttributes
//
//        if(StringUtils.hasLength(user.getUserName()) && "123456".equals(user.getPassword())){
//            //把登陆成功的用户保存起来
//            session.setAttribute("loginUser",user);
//            //登录成功重定向到main.html;  重定向防止表单重复提交
//            return "redirect:/main.html";
//        }else {
//            model.addAttribute("msg","账号密码错误");
//            //回到登录页面
//            return "login";
//        }
//    }
//
//    /**
//     * 去main页面
//     * @return
//     */
//    @GetMapping("/main.html")
//    public String mainPage(HttpSession session, Model model){
//
//        //最好用拦截器,过滤器
//        Object loginUser = session.getAttribute("loginUser");
//        if(loginUser != null){
//            return "main";
//        }else {
//            //session过期,没有登陆过
//            //回到登录页面
//            model.addAttribute("msg","请重新登录");
//            return "login";
//        }
//    }

}

4.运行项目,输入http://localhost:8080/,成功登录访问页面:

在这里插入图片描述

5.之后进行主页面的编辑(登录页面后跳转的页面就是主页面)

首先将index页面放入templates并改名为main:

在这里插入图片描述
找到之前的login.html:

lang="en"后添加xmlns:th="http://www.thymeleaf.org
action="index.html"后添加th语句:method="post" th:action="@{/login}"
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="ThemeBucket">
    <link rel="shortcut icon" href="#" type="image/png">

    <title>Login</title>

    <link href="css/style.css" rel="stylesheet">
    <link href="css/style-responsive.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="js/html5shiv.js"></script>
    <script src="js/respond.min.js"></script>
    <![endif]-->
</head>

<body class="login-body">

<div class="container">

    <form class="form-signin" action="index.html" method="post" th:action="@{/login}">
        <div class="form-signin-heading text-center">
            <h1 class="sign-title">Sign In</h1>
            <img src="images/login-logo.png" alt=""/>
        </div>
        <div class="login-wrap">
            <input type="text" class="form-control" placeholder="User ID" autofocus>
            <input type="password" class="form-control" placeholder="Password">

            <button class="btn btn-lg btn-login btn-block" type="submit">
                <i class="fa fa-check"></i>
            </button>

            <div class="registration">
                Not a member yet?
                <a class="" href="registration.html">
                    Signup
                </a>
            </div>
            <label class="checkbox">
                <input type="checkbox" value="remember-me"> Remember me
                <span class="pull-right">
                    <a data-toggle="modal" href="#myModal"> Forgot Password?</a>

                </span>
            </label>

        </div>

        <!-- Modal -->
        <div aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1" id="myModal" class="modal fade">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title">Forgot Password ?</h4>
                    </div>
                    <div class="modal-body">
                        <p>Enter your e-mail address below to reset your password.</p>
                        <input type="text" name="email" placeholder="Email" autocomplete="off" class="form-control placeholder-no-fix">

                    </div>
                    <div class="modal-footer">
                        <button data-dismiss="modal" class="btn btn-default" type="button">Cancel</button>
                        <button class="btn btn-primary" type="button">Submit</button>
                    </div>
                </div>
            </div>
        </div>
        <!-- modal -->

    </form>

</div>


<!-- Placed js at the end of the document so the pages load faster -->

<!-- Placed js at the end of the document so the pages load faster -->
<script src="js/jquery-1.10.2.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/modernizr.min.js"></script>

</body>
</html>

6.在IndexController中添加一下跳转页面语句:

    @PostMapping("/login")
    public String main(String username,String password){
        return "main";
    }
启动项目,测试启动情况

在这里插入图片描述

7.为进一步测试,在bean包下新建一个User类:

在这里插入图片描述
User.java

package com.it.springbootoracle.bean;

import lombok.Data;

@Data
public class User {
    private String userName;
    private String password;

	public String getUserName() {
        return userName;
    }

    public String getPassword() {
        return password;
    }
}
进入到login.html -> 在30行左右找到div class="login-wrap"字段,在其下方添加:
        <div class="login-wrap">
        	<label style="color: red" th:text="${msg}"></label>
            <input type="text" name="userName" class="form-control" placeholder="User ID" autofocus>
            <input type="password" name="password" class="form-control" placeholder="Password">
回到IndexController.java:
package com.it.springbootoracle.controller;

import com.it.springbootoracle.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
@Controller
public class IndexController {
    /**
     * 来登录页
     * @return
     */
    @GetMapping(value = {"/","/login"})
    public String loginPage(){

        return "login";
    }

    @PostMapping("/login")
    public String main(User user, HttpSession session, Model model){ //RedirectAttributes

        if(StringUtils.hasLength(user.getUserName()) && "123456".equals(user.getPassword())){
            //把登陆成功的用户保存起来
            session.setAttribute("loginUser",user);
            //登录成功重定向到main.html;  重定向防止表单重复提交
            return "redirect:/main.html";
        }else {
            model.addAttribute("msg","账号密码错误");
            //回到登录页面
            return "login";
        }
    }
    
    /**
     * 去main页面
     * @return
     */
    @GetMapping("/main.html")
    public String mainPage(HttpSession session, Model model){

        //最好用拦截器,过滤器
        Object loginUser = session.getAttribute("loginUser");
        if(loginUser != null){
            return "main";
        }else {
            //session过期,没有登陆过
            //回到登录页面
            model.addAttribute("msg","请重新登录");
            return "login";
        }
    }

}
测试登录的各种情况:

在这里插入图片描述

修改主页用户名

在main.html中找到John Doe名称(370行左右),用Thymeleaf的行内命名法进行替换

<a href="#" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
    <img src="images/photos/user-avatar.png" alt="" />
        [[${session.loginUser.userName}]]
    <span class="caret"></span>
</a>

ctrl+F9刷新html文件

进入到主界面,发现名称已经更新。

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
javaweb视图层设计开发实验是一项重要的实践性任务,通过该实验可以很好地掌握javaweb视图层的设计和开发技能。在实验中,我们需要在MVC架构的基础上,使用JSP和Servlet技术进行视图层的设计和开发,具体的实验小结如下: 一、实验目的 1. 掌握JSP的基本语法和标签,能够使用JSP实现页面的动态渲染和数据展示。 2. 掌握Servlet的基本原理和使用方法,能够使用Servlet处理HTTP请求和响应。 3. 掌握MVC架构的基本概念和设计思想,能够将视图层和业务层进行分离。 二、实验过程 1. 首先,需要对实验要求进行了解和分析,理解MVC架构的基本概念和设计思想,明确视图层、业务层和数据层之间的关系。 2. 然后,根据实验要求,设计并创建数据库表结构。 3. 接着,编写JavaBean类,用于与数据库进行交互,实现数据的增删改查等操作。 4. 在此基础上,编写Servlet类,用于处理HTTP请求和响应,将请求转发给业务层,获取处理结果后将数据传递给JSP页面进行渲染和展示。 5. 最后,编写JSP页面,使用JSP标签和EL表达式实现页面的动态渲染和数据展示。 三、实验收获 通过本次实验,我深刻理解了MVC架构的基本概念和设计思想,掌握了JSP和Servlet的基本语法和使用方法,能够使用JavaBean类与数据库进行交互,实现数据的增删改查等操作,同时也能够使用JSP页面进行动态渲染和数据展示。这对我的后续学习和工作都有很大的帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jay_fearless

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值