Spring Boot中实现简单表单提交(登录功能)

140 篇文章 4 订阅

目录

 

原理

源码


 

原理

使用@PostMapping可以得到前端Post的Mapping!

@RequestParam可以获取详细的参数信息;

程序运行截图如下(登录成功):

 

程序运行截图如下(登录失败):

 

源码

程序结构如下:

源码如下:

MyMvcConfig.java

package firstlogindemo.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    @Bean
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){

        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {

                registry.addViewController("/").setViewName("index");
                registry.addViewController("index.html").setViewName("index");
            }
        };

        return adapter;
    }
}

LoginController.java

package firstlogindemo.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;

import java.util.Map;

@Controller
public class LoginController {

    //@DeleteMapping
    //@PutMapping
    //@GetMapping
    //@RequestMapping(value = "/usr/login", method = RequestMethod.POST)

    @PostMapping(value = "/user/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Map<String, Object> map){

        if(!StringUtils.isEmpty(username) && "123456".equals(password)){

            return "success";
        }

        map.put("msg", "用户名密码错误");
        return "index";
    }
}

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<form th:action="@{/user/login}" method="post">
    <p>用户名:<input name="username" type="text" placeholder="userName"></p>
    <p>密  码:<input name="password" type="password" placeholder="Password"></p>
    <button type="submit">提交</button>
</form>

</body>
</html>

success.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>成功</h1>

</body>
</html>

porm.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.19.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.loginWebDemo</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>loginWeb</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!--引入jquery-webjar-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.3.1</version>
        </dependency>

        <!--引入bootstrap-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.0.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

  • 20
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
实现Spring Boot和Vue的登录功能,可以按照以下步骤进行操作: 1. 在Spring Boot创建一个控制器(Controller),用于处理登录请求。 2. 创建一个用于存储用户信息的数据库表。 3. 在Vue创建一个登录页面,包括用户名和密码输入框以及登录按钮。 4. 使用axios库将表单数据发送到Spring Boot后端。 5. 在Spring Boot编写服务端验证逻辑,验证用户名和密码是否正确。 6. 如果验证通过,返回带有用户信息的JSON对象。 7. 在Vue接收服务端返回的JSON对象,并根据用户信息跳转到相应的页面。 下面是一个简单的示例代码: Spring Boot控制器代码: ```java @RestController @RequestMapping("/api") public class LoginController { @Autowired private UserRepository userRepository; @PostMapping("/login") public ResponseEntity<User> login(@RequestParam String username, @RequestParam String password) { User user = userRepository.findByUsername(username); if (user != null && user.getPassword().equals(password)) { return ResponseEntity.ok(user); } else { return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); } } } ``` Vue登录页面代码: ```html <template> <div> <input v-model="username" type="text" placeholder="用户名"> <input v-model="password" type="password" placeholder="密码"> <button @click="login">登录</button> </div> </template> <script> import axios from 'axios' export default { data() { return { username: '', password: '' } }, methods: { login() { axios.post('/api/login', { username: this.username, password: this.password }).then(response => { if (response.status === 200) { // 登录成功,跳转到主页 this.$router.push('/main') } else { alert('用户名或密码错误') } }).catch(error => { alert('登录失败,请重试') }) } } } </script> ``` 其,UserRepository是一个Spring Data JPA的Repository,用于操作数据库表。在上面的代码,使用findByUsername方法查找用户名对应的用户对象。如果找到了用户对象,并且用户输入的密码与数据库存储的密码相同,就返回带有用户信息的JSON对象;否则,返回HTTP 401状态码表示认证失败。在Vue,使用axios库发送POST请求,将表单数据包装成JSON对象,然后接收服务端返回的JSON对象。如果登录成功,就跳转到主页;否则,显示错误信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

IT1995

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

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

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

打赏作者

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

抵扣说明:

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

余额充值