一登錄界面 view=>>>>>>>componentjavascript
import React ,{Component} from 'react'; import { bindActionCreators } from 'redux'; import { Link } from 'react-router-dom'; import {Navbar,Nav,NavItem ,NavDropdown ,MenuItem} from "react-bootstrap"; require(`./style.less`) import { connect } from 'react-redux'; import * as loginvolidate from 'actions/loginvolidate'; @connect ( state => state, dispatch => bindActionCreators(loginvolidate, dispatch) ) class Header extends React.Component { constructor(props) { super(props); //驗證每次組件發生變化時都會驗證 this.props.loginVolidate_success() } render() { const {data} = this.props.loginvolidate; console.log(data) return ( <div> //登錄用戶名設置頁面轉換 {data==undefined? <Navbar inverse collapseOnSelect> <Navbar.Header> <Navbar.Brand> 狠林首頁 </Navbar.Brand> <Navbar.Toggle /> </Navbar.Header> <Navbar.Collapse> <Nav pullRight> <NavItem eventKey={1} ><Link to='login'>登錄</Link></NavItem> <NavItem eventKey={2} ><Link to='register'>註冊</Link></NavItem> </Nav> </Navbar.Collapse> </Navbar> : <Navbar inverse collapseOnSelect> <Navbar.Header> <Navbar.Brand> 狠林首頁 </Navbar.Brand> <Navbar.Toggle /> </Navbar.Header> <Navbar.Collapse> <Nav bsStyle="pills" pullRight > <NavDropdown eventKey="4" title={data} id="nav-dropdown"> <MenuItem eventKey="4.1">我的中心</MenuItem> <MenuItem eventKey="4.2">會員</MenuItem> <MenuItem eventKey="4.3">發帖管理</MenuItem> </NavDropdown> <NavItem eventKey="2" title="Item">消息</NavItem> <NavItem eventKey="1" title="Item">註銷</NavItem> </Nav> </Navbar.Collapse> </Navbar> } </div> ) } } export default Header;
二 actionsjava
import fetch from 'isomorphic-fetch'; import queryString from 'queryString' export const success = (data) => ({ type: 'loginVolidate_success', data:data.data }) // 頁面初次渲染時獲取數據 export const loginVolidate_success = () => { return dispatch => { //這裏須要在server.js中設置攔截 不然寫全http:localhost:8080/dog/login/loginVolidate.do fetch('/dog/login/loginVolidate.do', { credentials: 'include', //必須加上用來與後臺session進行交互,不然,會發現後臺session爲null method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', }, body: queryString.stringify() }) .then((response) => { if(response.ok){ return response.json(); } }) .then(data=>{ dispatch(success(data)) }) .catch(()=>alert('用戶名或密碼錯誤')) } }
三reduxreact
const initState = { } export const loginvolidate = (state = initState, action) => { switch (action.type) { case "loginVolidate_success": return { ...state, data: action.data } default: return state } }
store與相關配置web
後臺:springmvcspring
package com.Tieba.controller; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.SessionAttributes; import dog.User; @Controller @RequestMapping("/login") public class loginController { @RequestMapping(value="/loginUser", method=RequestMethod.POST) @ResponseBody public Map<String, Object> loginUser(HttpServletRequest req,HttpServletResponse res, HttpSession session){ System.out.println(req.getParameter("user")); System.out.println(req.getParameter("psw")); session.setAttribute("user", req.getParameter("user")); Map<String,Object> map = new HashMap<String, Object>(); map.put("data", "success"); return map; } @RequestMapping(value="/loginVolidate", method=RequestMethod.POST) @ResponseBody public Map<String, Object> loginVolidate(HttpServletRequest req,HttpSession session){ Map<String,Object> map = new HashMap<String, Object>(); map.put("data", session.getAttribute("user")); System.out.println(session.getAttribute("user")); return map; } }