zoukankan      html  css  js  c++  java
  • react+antd+springmvc 实现简单的登录功能

     react开发工具create-react-app+vscode
    (1)第一步,配置tomcat项目可跨域访问
    我的react项目端口是3000,java的项目端口是8080,所以第一步实现跨域访问(请参考地址http://blog.csdn.net/u012500848/article/details/51162449自己实现)
    (2)springmvc后台登录代码
        @RequestMapping(value = { "/test/login" }, method = RequestMethod.POST)
        @ResponseBody
        public String Login(HttpServletRequest request) {
            String username = request.getParameter("username");
            String pwd = request.getParameter("userpwd");
            if (username == null || username.isEmpty()) {
                return "帐号为空";
            }
            if (pwd == null || pwd.isEmpty()) {
                return "密码为空";
            }
            if (username.equals("test") && pwd.equals("123456")) {
                return "登录成功";
            } else {
                return "帐号或密码错误";
            }
        }

    (3)使用fetch实现登录功能,要先在项目中安装fetch(yarn add whatwg-fetch)

    import React from 'react';
    import {Form, Icon, Input, Button, message} from 'antd';
    import 'whatwg-fetch';
    import './Login.css';
    const FormItem = Form.Item; class Login extends React.Component { //登录事件 handleSubmit = (e) => { e.preventDefault(); let url = "http://localhost:8080/shiro/test/login"; let formData = new FormData(); formData.append('username', this.props.form.getFieldValue("username")); formData.append('userpwd', this.props.form.getFieldValue("userpwd")); fetch(url, { method: 'post', mode: 'cors', body: formData }).then(function (response) { return response.text() }).then(function (body) { message.info(body); }); } render() { const {getFieldDecorator} = this.props.form; return ( <Form onSubmit={this.handleSubmit} className="login-form"> <FormItem> {getFieldDecorator('username', {})( <Input prefix={< Icon type = "user" style = {{ fontSize: 13 }}/>} placeholder="帐号"/> )} </FormItem> <FormItem> {getFieldDecorator('userpwd', {})( <Input prefix={< Icon type = "lock" style = {{ fontSize: 13 }}/>} type="password" placeholder="密码"/> )} </FormItem> <FormItem> <Button type="primary" htmlType="submit" className="login-form-button"> 登录 </Button> </FormItem> </Form> ); } } const WrappedNormalLoginForm = Form.create()(Login); export default WrappedNormalLoginForm;
    this.props.form.getFieldValue方法用来获取input输入的值。

    (4)实现效果

  • 相关阅读:
    FlatBuffers要点
    tarjan+缩点+强连通定理
    编程之美2.16 最长递增子序列
    Android Studio之多个Activity的滑动切换(二)
    Effective java读书札记第一条之 考虑用静态工厂方法取代构造器
    【PM】关于系统数据库和服务现场升级的一些看法
    用户及权限基础 2---- 权限
    Android双向滑动菜单完全解析,教你如何一分钟实现双向滑动特效
    【转贴】gdb中的信号(signal)相关调试技巧
    基于新浪sae使用php生成图片发布图文微博
  • 原文地址:https://www.cnblogs.com/mydotnetforyou/p/7839923.html
Copyright © 2011-2022 走看看