VueSPA(项目登陆前后台实现)

一路踩坑的博主):一定要细心不然BUG会让你活活气死

SPA项目流程

      • 1.vue+elementUI完成注册及登陆
      • 2. Vue+ElementUI设计登陆页面
      • 3. 后台交互(axios/qs/vue-axios)
      • 4.后台实现流程:
          • 后台util工具类提供:

1.vue+elementUI完成注册及登陆

1.1. vue怎么引入和配置使用element-ui框架
1.1.1 使用vue-cli脚手架工具创建一个vue项目
vue init webpack pro01

1.1.2 npm安装elementUI
cd pro01 #进入新建项目的根目录
npm install element-ui -S #安装element-ui模块

##重要的事情说三遍:在指定位置!!!在指定位置!!!在指定位置!!!~~~添加三行代码
1.1.3 在项目中src目录下找到main.js,并在指定位置添加三行代码(main.js是入口文件,所以在这里引入就行,页面就不用引入了)
import Vue from ‘vue’

import ElementUI from ‘element-ui’ //新添加1
import ‘element-ui/lib/theme-chalk/index.css’ //新添加2,避免后期打包样式不同,要放在import App from ‘./App’;之前

import App from ‘./App’
import router from ‘./router’

Vue.use(ElementUI) //新添加3
Vue.config.productionTip = false
VueSPA(项目登陆前后台实现)_第1张图片

1.1.4 测试
修改HelloWorld.vue添加elementUI组件查看效果
VueSPA(项目登陆前后台实现)_第2张图片

2. Vue+ElementUI设计登陆页面

注1:相关样式见资料“css.txt”

注2:

注2:auto-complete=“off”
autocomplete 属性是 HTML5 中的新属性,off-----禁用自动完成

登陆页面源码(一定要配置好VUE ELEMENT-UI组件):

	
	
	


实现登陆页面需要配置router/index.js:

	import Vue from 'vue'
	import Router from 'vue-router'
	import Login from '@/views/Login'
	import Main from '@/views/Main'
	import AppMain from '@/vue/AppMain'//动态树页面
	import User from '@/vue/User'//动态树子页面
	import Uilr from '@/vue/Uilr'//动态树子页面
	import Role from '@/vue/Role'//动态树子页面
	
	
	
	Vue.use(Router)
	
	export default new Router({
	  routes: [
			{
				path: '/',
				name: 'Main',
				component: Main
			},
			{
				path: '/Login',
				name: 'Login',
				component: Login
			},
			{
				path: '/AppMain',
				name: 'AppMain',
				component: AppMain,
	      children:[
	        {
	          path: 'Uilr',
	          name: 'Uilr',
	          component: Uilr
	      },{
	          path: 'User',
	          name: 'User',
	          component: User
	      },{
	          path: 'Role',
	          name: 'Role',
	          component: Role
	      }]
			}]
	})

在cmd输入:npm run dev启动项目在这里插入图片描述

VueSPA(项目登陆前后台实现)_第3张图片

3. 后台交互(axios/qs/vue-axios)

3.1 axios
axios是vue2提倡使用的轻量版的ajax。它是基于promise的HTTP库。它会从浏览器中创建XMLHttpRequests,与Vue配合使用非常好。

  1.题外话:
  vue.js有著名的全家桶系列:vue-router,vuex, vue-resource,再加上构建工具vue-cli,就是一个完整的vue项目的核心构成。
  其中vue-resource是Vue.js的一款插件,它可以通过XMLHttpRequest或JSONP发起请求并处理响应,但在vue更新到2.0之后,
  作者就宣告不再对vue-resource更新,而是推荐的axios

  2.GET提交
  axios.get('/user', {//注意数据是保存到json对象的params属性
    params: {
      ID: 12345
    }
  }).then((response)=> {
    console.log(response);
  }).catch((error)=>  {
    console.log(error);
  });

  3.POST提交
  axios.post('/user', {//注意数据是直接保存到json对象
    firstName: 'Fred',
    lastName: 'Flintstone'
  }).then((response)=> {
    console.log(response);
  }).catch((error) =>{
    console.log(error);
  });
  
  ==登陆页面源码里面直接可以参考:==
  ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200510213106867.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTA2NzEyMA==,size_16,color_FFFFFF,t_70)

  注1:axios跨域问题
       会一直报错:“http://127.0.0.1:8848' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header”
       因为使用了前后端分离开发,跨域访问了,解决方案:需要配置tomcat允许跨域访问,
       tomcat跨域配置方法很多,但最简单的方式是自己写一个过滤器CorsFilter实现,添加一个响应头
       Access-Control-Allow-Origin即可
       httpResponse.addHeader("Access-Control-Allow-Origin", "*");//*表示任何域名
       httpResponse.addHeader("Access-Control-Allow-Origin", "http://localhost:80"); 

  Access-Control-Allow-Origin:*                           #则允许所有域名的脚本访问该资源。
   Access-Control-Allow-Origin:https://www.fujieace.com    #允许特定的域名访问 

CorsFilter源码:

package com.zking.login.sys.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 配置tomcat允许跨域访问
 * 
 * @author Administrator
 *
 */
public class CorsFilter implements Filter {

@Override
public void init(FilterConfig filterConfig) throws ServletException {
}

// @Override
// public void doFilter(ServletRequest servletRequest, ServletResponse
// servletResponse, FilterChain filterChain)
// throws IOException, ServletException {
// HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
//
// // Access-Control-Allow-Origin就是我们需要设置的域名
// // Access-Control-Allow-Headers跨域允许包含的头。
// // Access-Control-Allow-Methods是允许的请求方式
// httpResponse.addHeader("Access-Control-Allow-Origin", "*");// *,任何域名
// httpResponse.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT,
// DELETE");
// // httpResponse.setHeader("Access-Control-Allow-Headers", "Origin,
// // X-Requested-With, Content-Type, Accept");
//
// // 允许请求头Token
// httpResponse.setHeader("Access-Control-Allow-Headers",
// "Origin,X-Requested-With, Content-Type, Accept, Token");
// HttpServletRequest req = (HttpServletRequest) servletRequest;
// System.out.println("Token=" + req.getHeader("Token"));
// if("OPTIONS".equals(req.getMethod())) {
// return;
// }
//
//
// filterChain.doFilter(servletRequest, servletResponse);
// }

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
		throws IOException, ServletException {
	HttpServletResponse resp = (HttpServletResponse) servletResponse;
	HttpServletRequest req = (HttpServletRequest) servletRequest;

	// Access-Control-Allow-Origin就是我们需要设置的域名
	// Access-Control-Allow-Headers跨域允许包含的头。
	// Access-Control-Allow-Methods是允许的请求方式
	resp.setHeader("Access-Control-Allow-Origin", "*");// *,任何域名
	resp.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE");
	resp.setHeader("Access-Control-Allow-Headers", "Origin,X-Requested-With, Content-Type, Accept");

	// 允许请求头Token
	// httpResponse.setHeader("Access-Control-Allow-Headers","Origin,X-Requested-With,
	// Content-Type, Accept, Token");
	// System.out.println("Token=" + req.getHeader("Token"));

	if ("OPTIONS".equals(req.getMethod())) {// axios的ajax会发两次请求,第一次提交方式为:option,直接返回即可
		return;
	}
	filterChain.doFilter(servletRequest, servletResponse);
}

@Override
public void destroy() {

}
}

在Web.xml配置:(后面会提供源码)
VueSPA(项目登陆前后台实现)_第4张图片

注2:axios.get提交没有问题,axios.post提交后台接收不到数据
因为POST提交的参数的格式是Request Payload,这样后台取不到数据的(详情见资料“05 Vue中axios踩坑之路-POST传参 - RainSun - CSDN博客.mht”),
解决方案:使用qs.js库,将{a:‘b’,c:‘d’}转换成’a=b&c=d’
VueSPA(项目登陆前后台实现)_第5张图片

注3:为简化axios使用,还可以使用axios全局配置及拦截器,详情见资料“api/http.js”
axios.defaults.baseURL = ‘https://api.example.com’;
//axios.defaults.headers.common[‘Authorization’] = AUTH_TOKEN;//自定义请求头,添加认证令牌
axios.defaults.headers.post[‘Content-Type’] = ‘application/x-www-form-urlencoded’;

http.js源码:

	/**
	 * vue项目对axios的全局配置
	 */
	import axios from 'axios'
	import qs from 'qs'
	
	//引入action模块,并添加至axios的类属性urls上
	import action from '@/api/action'
	axios.urls = action
	
	// axios默认配置
	axios.defaults.timeout = 10000; // 超时时间
	// axios.defaults.baseURL = 'http://localhost:8080/j2ee15'; // 默认地址
	axios.defaults.baseURL = action.SERVER;
	
	//整理数据
	// 只适用于 POST,PUT,PATCH,transformRequest` 允许在向服务器发送前,修改请求数据
	axios.defaults.transformRequest = function(data) {
		data = qs.stringify(data);
		return data;
	};
	
	
	// 请求拦截器
	axios.interceptors.request.use(function(config) {
		return config;
	}, function(error) {
		return Promise.reject(error);
	});
	
	// 响应拦截器
	axios.interceptors.response.use(function(response) {
		return response;
	}, function(error) {
		return Promise.reject(error);
	});
	
	// // 路由请求拦截
	// // http request 拦截器
	// axios.interceptors.request.use(
	// 	config => {
	// 		//config.data = JSON.stringify(config.data);  
	// 		//config.headers['Content-Type'] = 'application/json;charset=UTF-8';
	// 		//config.headers['Token'] = 'abcxyz';
	// 		//判断是否存在ticket,如果存在的话,则每个http header都加上ticket
	// 		// if (cookie.get("token")) {
	// 		// 	//用户每次操作,都将cookie设置成2小时
	// 		// 	cookie.set("token", cookie.get("token"), 1 / 12)
	// 		// 	cookie.set("name", cookie.get("name"), 1 / 12)
	// 		// 	config.headers.token = cookie.get("token");
	// 		// 	config.headers.name = cookie.get("name");
	// 		// }
	// 		return config;
	// 	},
	// 	error => {
	// 		return Promise.reject(error.response);
	// 	});
	
	// // 路由响应拦截
	// // http response 拦截器
	// axios.interceptors.response.use(
	// 	response => {
	// 		if (response.data.resultCode == "404") {
	// 			console.log("response.data.resultCode是404")
	// 			// 返回 错误代码-1 清除ticket信息并跳转到登录页面
	// 			//      cookie.del("ticket")
	// 			//      window.location.href='http://login.com'
	// 			return
	// 		} else {
	// 			return response;
	// 		}
	// 	},
	// 	error => {
	// 		return Promise.reject(error.response) // 返回接口返回的错误信息
	// 	});

export default axios;

注4:为方便开发,建议对请求地址进行封装,详情见资料“api/action.js”
action.js源码:

 /**
 * 对后台请求的地址的封装,URL格式如下:
 * 模块名_实体名_操作
 */
export default {
	//服务器
	'SERVER': 'http://localhost:8080/vueSPA',

	//用户管理
	'SYS_USER_LOGIN': '/sys/userAction.action', //登陆
	/* 'SYS_USER_DOREG': '/sys/userAction_doReg.action', //注册
	'SYS_USER_LIST': '/sys/userAction_list.action', //用户查询
	'SYS_USER_DORESETPWD': '/sys/userAction_doResetPwd.action', //重置密码 */

	//文章管理
	'SYS_ARTICLES_ADD': '/sys/articlesAction_add.action',
	'SYS_ARTICLES_EDIT': '/sys/articlesAction_edit.action',
	'SYS_ARTICLES_DEL': '/sys/articlesAction_del.action',
	'SYS_ARTICLES_LOAD': '/sys/articlesAction_load.action',
	'SYS_ARTICLES_LIST': '/sys/articlesAction_list.action',

	//菜单管理
	'SYS_TREENODE_BOOTS': '/sys/TreeNodeAction_Boots.action',//动态树action

	//获得请求的完整地址,用于mockjs测试时使用
	'getFullPath': k => {
		return this.SERVER + this[k];
	}
}

注5:_~~~~为进一步简化开发,将action.js的URL地址封装到axios.urls属性上

VueSPA(项目登陆前后台实现)_第6张图片
3.2 qs

  qs.js它是一个url参数转化的js库。用法就两个:
  var obj = qs.parse('a=b&c=d');  //将URL解析成对象的形式:{a:'b',c:'d'}
  var str = qs.stringify(obj);    //将对象 序列化成URL的形式,以&进行拼接:a=b&c=d'

3.3 vue-axios
vue-axios是在axios基础上扩展的插件,在Vue.prototype原型上扩展了$http等属性,可以更加方便的使用axios

3.4 axios/qs/vue-axios安装及使用步骤
1.安装
npm install axios -S
npm install qs -S
npm install vue-axios -S

2.修改main.js文件
#import axios from ‘axios’
#import qs from ‘qs’
import axios from ‘@/api/http’ #vue项目对axios的全局配置
import VueAxios from ‘vue-axios’
Vue.use(VueAxios,axios)
VueSPA(项目登陆前后台实现)_第7张图片

3.之后就可以直接在Vue组件文件的methods里使用了
this.axios.get(url,{params:{id:1,name:‘zs’}}).then((response)=>{
this.newsList=response.data.data;
}).catch((response)=>{
console.log(response);
});

VueSPA(项目登陆前后台实现)_第8张图片
附录一:ES6的语法箭头函数,详情见资料“03 箭头函数 - 廖雪峰的官方网站.mht”
doSomething(function(a){…});
doSomething((a)=>{});
VueSPA(项目登陆前后台实现)_第9张图片

4.后台实现流程:

-----Postman //测试
Action UserAction//功能层
-----junit //测试
service IUserService UserService//服务层
dao IUserDAO UserDao //DAO层
model User.hbm.xml //实体类映射文件
db //数据库

源码后面有:
VueSPA(项目登陆前后台实现)_第10张图片

spring.base.xml配置连接池
VueSPA(项目登陆前后台实现)_第11张图片

实体类源码:

package com.zking.login.sys.model;

import java.io.Serializable;
import java.sql.Timestamp;

import com.zking.login.base.model.BaseModel;

public class LoginUser extends BaseModel {
	
private Long userid;
private String username;
private String password;

private String salt;
private Integer locked;
private Timestamp createDatetime;

public LoginUser() {
}

public Long getUserid() {
	return userid;
}

public void setUserid(Long userid) {
	this.userid = userid;
}

public String getUsername() {
	return username;
}

public void setUsername(String username) {
	this.username = username;
}

public String getPassword() {
	return password;
}

public void setPassword(String password) {
	this.password = password;
}

public String getSalt() {
	return salt;
}

public void setSalt(String salt) {
	this.salt = salt;
}

public Integer getLocked() {
	return locked;
}

public void setLocked(Integer locked) {
	this.locked = locked;
}

public Timestamp getCreateDatetime() {
	return createDatetime;
}

public void setCreateDatetime(Timestamp createDatetime) {
	this.createDatetime = createDatetime;
}

@Override
public String toString() {
	return "LoginUser [userid=" + userid + ", username=" + username + ", password=" + password + ", salt=" + salt
			+ ", locked=" + locked + ", createDatetime=" + createDatetime + "]";
}
}

实体映射文件源码:




	
		
			
		
		
		
		
		
		
	

VueSPA(项目登陆前后台实现)_第12张图片

UserDAO源码:

package com.zking.login.sys.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.HibernateCallback;

import com.zking.login.base.dao.BaseDao;
import com.zking.login.sys.dao.IUserDao;
import com.zking.login.sys.model.LoginUser;

public class UserDaoimpl extends BaseDao implements IUserDao {

@Override
public LoginUser load(LoginUser user) {
	return this.getHibernateTemplate().execute(new HibernateCallback() {
		@Override
		public LoginUser doInHibernate(Session session) throws HibernateException {
			Query query = session.createQuery("from LoginUser u where u.username = :username");//查询出结果放进json中
			query.setString("username", user.getUsername());//把json转为string类型
			LoginUser u = (LoginUser) query.uniqueResult();//query内部一个方法
			return u;
		}
	});
}

}

继承类源码BaseDao:

package com.zking.login.base.dao;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import com.zking.login.base.util.PageBean;


public class BaseDao extends HibernateDaoSupport implements Serializable {

private static final long serialVersionUID = 1619854797335382396L;

private Logger log = LogManager.getLogger(BaseDao.class);

public BaseDao() {
	super();
}

/**
 * 通用赋值方法
 * 
 * @param query
 * @param args
 */
@SuppressWarnings({ "unused", "rawtypes" })
private void setParameters(Query query, Map args) {
	if (null == args || 0 == args.size()) {
		return;
	}
	log.info("hql查询参数为:" + args);

	String name = null;
	Object value = null;
	for (Map.Entry entry : args.entrySet()) {
		name = entry.getKey();// min,uname
		value = entry.getValue();// 1000f,'z%'
		// query.setFloat(name, value);
		// query.setString(name, value);
		// query.setParameter(param, value);//8+1
		if (value instanceof Collection) {// List和Set
			query.setParameterList(name, (Collection) value);
		} else if (value instanceof Object[]) {
			query.setParameterList(name, (Object[]) value);
		} else {
			query.setParameter(name, value);
		}
	}
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public List executeQuery(final String hql, final Map args, final PageBean pageBean) {
	// Session session =
	// this.getHibernateTemplate().getSessionFactory().openSession();

	return (List) this.getHibernateTemplate().execute(new HibernateCallback() {
		@Override
		public Object doInHibernate(Session session) throws HibernateException {
			// 1. 查满足条件的总记录数
			Query query = null;
			if (null != pageBean && pageBean.isPagination()) {
				String countHql = getCountHql(hql);
				log.info("countHql[" + countHql + "]");
				query = session.createQuery(countHql);
				setParameters(query, args);// 给占位符赋值
				Object total = query.uniqueResult();
				pageBean.setTotal(total.toString());
			}
			// 2. 查满足条件并指定页码 的记录
			log.info("countHql[" + hql + "]");
			query = session.createQuery(hql);
			if (null != pageBean && pageBean.isPagination()) {
				query.setFirstResult(pageBean.getStartIndex());
				query.setMaxResults(pageBean.getRows());
			}
			setParameters(query, args);// 给占位符赋值
			List list = query.list();
			return list;
		}
	});
}
// from Account a where 1=1 OrDer by a.balance desc
// from Account a where 1=1
// select a.userName from Account a where 1=1 OrDer by a.balance desc
// select count(*) from Account a where 1=1

/**
 * 将普通的hql转换成查总记录数的hql
 * 
 * @param hql
 * @return
 */
private static String getCountHql(String hql) {
	String newHql = new String(hql).toUpperCase();
	int start = newHql.indexOf("FROM ");
	int end = newHql.indexOf("ORDER BY");
	if (-1 == end) {
		newHql = "select count(*) " + hql.substring(start);
	} else {
		newHql = "select count(*) " + hql.substring(start, end);
	}
	return newHql;
}

// public static void main(String[] args) {
// String hql2 = "from Order o where o.orderId in (1,2,3,5)";// (:orderIds)
// String hql = "select a.userName from Account a where 1=1 and a.balance >=
// :min order by a.balance";
// System.out.println(getCountHql(hql));
// }
}

接口类IUserDAO:
VueSPA(项目登陆前后台实现)_第13张图片

UserService:

package com.zking.login.sys.service.impl;

import java.util.List;

import com.zking.login.base.service.BaseService;
import com.zking.login.sys.dao.IUserDao;
import com.zking.login.sys.model.LoginUser;
import com.zking.login.sys.service.IUserService;
import com.zking.login.sys.util.PageBean;

public class UserService extends BaseService  implements IUserService{
	
private IUserDao userdao;

public UserService() {
}
public IUserDao getUserdao() {
	return userdao;
}

public void setUserdao(IUserDao userdao) {
	this.userdao = userdao;
}

@Override
public LoginUser load(LoginUser loginUser) {
	return userdao.load(loginUser);
}

}

IUserSerivce:
VueSPA(项目登陆前后台实现)_第14张图片
把DAO,Service配置到Spring(容器)中:

spring.sys.xml:



	
	
	
	
		
			
		
	
	
	
	
	
		
			
		
	
	
	
	 
		
			
		
	
	
	
		
			
		
	 

spring.base.xml:



		
	
	
		
		
		
		

		
		

		
		

		
		

		
		

		
		

		
		

		
		

		
		

		
		

		
		
	

	
	
		
		
			
		
		
		
			
				org.hibernate.dialect.MySQLDialect
				true
				true
				true
				true
				org.hibernate.cache.ehcache.EhCacheRegionFactory
				
				true
			
		
		
		
			
				mapping/LoginUser.hbm.xml
				mapping/TreeNode.hbm.xml
			
		
	

	
	
		
			
		
	

	
	
	

	
	
		
	

	
	
	
		
			
			
			
			
			
			
		
	

	
	
	
		
		
		
		
		
		
		
	
	
	
	
	
		
			
		
	

配置到spring中:



	
	
	

junit测试:

VueSPA(项目登陆前后台实现)_第15张图片
IUserServiceTest :

package com.zking.login.sys.service;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import com.zking.login.sys.model.LoginUser;
import com.zking.login.sys.service.impl.BaseTestCase;

public class IUserServiceTest extends BaseTestCase {

private IUserService userservice;
	
	private LoginUser user;
	
	@Before
	public void before() {
		user = new LoginUser();
		userservice = (IUserService) this.getBean("userService");
	}

	@Test
	public void testLoad() {
		user.setUsername("zs");
		LoginUser u = userservice.load(user);
		System.out.println(u);
	}

}

继承类BaseTestCase:

package com.zking.login.sys.service.impl;

import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class BaseTestCase {
	private static ApplicationContext context;
	static {
		context =new ClassPathXmlApplicationContext("classpath:spring.xml");
	}
	protected Object getBean(String name) {
		return context.getBean(name);
	} 
}

测试结果:
VueSPA(项目登陆前后台实现)_第16张图片

然后写UserAction:

package com.zking.login.sys.action;

import com.opensymphony.xwork2.ModelDriven;
import com.zking.login.base.action.BaseAction;
import com.zking.login.sys.model.LoginUser;
import com.zking.login.sys.service.IUserService;
import com.zking.login.sys.util.JsonData;

public class UserAction extends BaseAction implements ModelDriven{
	
	private static final long serialVersionUID = 1L;

	private IUserService userService;
	
	private LoginUser user = new LoginUser();
	
	public UserAction() {
		
	}
	public IUserService getUserService() {
		return userService;
	}
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	@Override
	public LoginUser getModel() {
		return user;
	}
	
	public String execute() {
		System.out.println(request.getMethod());
		System.out.println("后台Action"+this.user);
		JsonData jsonData = new JsonData();
		
		LoginUser u = userService.load(user);
		if(u != null && u.getPassword().equals(user.getPassword())) {
			jsonData.setCode(0);
			jsonData.setMessage("登陆成功");
		}else {
			jsonData.setCode(-1);
			jsonData.setMessage("账号或密码错误");
		}
		this.writeJson(jsonData);
		return null;
	}
}

配置Action到spring.sys.xml配置文件中:
VueSPA(项目登陆前后台实现)_第17张图片

启动Tomcat发布项目使用Postman测试:
VueSPA(项目登陆前后台实现)_第18张图片
成功后:
1)启动前端vue项目
-----MVVM(前后台分离)
2)启动Tomcat后台项目
VueSPA(项目登陆前后台实现)_第19张图片
VueSPA(项目登陆前后台实现)_第20张图片

VueSPA(项目登陆前后台实现)_第21张图片
成功前后台分离成功(下一集:动态树后台实现)

后台util工具类提供:

1.中文乱码器 EncodingFiter:

package com.zking.login.sys.util;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 中文乱码处理
 * 
 */
public class EncodingFiter implements Filter {

	private String encoding = "UTF-8";// 默认字符集

	public EncodingFiter() {
		super();
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;

		// 中文处理必须放到 chain.doFilter(request, response)方法前面
		res.setContentType("text/html;charset=" + this.encoding);
		if (req.getMethod().equalsIgnoreCase("post")) {
			req.setCharacterEncoding(this.encoding);
		} else {
			Map map = req.getParameterMap();// 保存所有参数名=参数值(数组)的Map集合
			Set set = map.keySet();// 取出所有参数名
			Iterator it = set.iterator();
			while (it.hasNext()) {
				String name = (String) it.next();
				String[] values = (String[]) map.get(name);// 取出参数值[注:参数值为一个数组]
				for (int i = 0; i < values.length; i++) {
					values[i] = new String(values[i].getBytes("ISO-8859-1"),
							this.encoding);
				}
			}
		}

		chain.doFilter(request, response);
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		String s = filterConfig.getInitParameter("encoding");// 读取web.xml文件中配置的字符集
		if (null != s && !s.trim().equals("")) {
			this.encoding = s.trim();
		}
	}

	}

2.服务器返回给客户端的JSONDATA格式的数据:

package com.zking.login.sys.util;

import java.io.Serializable;
import java.util.HashMap;

/**
 * 服务器返回给客户端的JSON格式的数据
 *
 */
public class JsonData extends HashMap implements Serializable {

	private static final long serialVersionUID = -8855960778711040221L;

	private static final String CODE_KEY = "code";// 操作代码 0 成功 非0 失败
	private static final String MESSAGE_KEY = "message";// 操作消息
	private static final String RESULT_KEY = "result";// 结果集
	private static final String PAGE_KEY = "page";// 页码
	private static final String ROWS_KEY = "rows";// 每页行数/页大小
	private static final String TOTAL_KEY = "total";// 总记录数

	public JsonData() {
		super();
		this.put(CODE_KEY, 0);// 默认操作成功
	}

	public void setCode(Integer code) {
		this.put(CODE_KEY, code);
	}

	public void setMessage(String message) {
		this.put(MESSAGE_KEY, message);
	}

	public void setResult(Object result) {
		this.put(RESULT_KEY, result);
	}

	public void setPage(Integer page) {
		this.put(PAGE_KEY, page);
	}

	public void setRows(Integer rows) {
		this.put(ROWS_KEY, rows);
	}

	public void setTotal(Integer total) {
		this.put(TOTAL_KEY, total);
	}

	}

3.后期用到的分页PageBean :

package com.zking.login.sys.util;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class PageBean {

	private int page = 1;// 页码
	private int rows = 10;// 行数/页大小
	private int total = 0;// 总记录数

	private boolean pagination = true;// 默认分页

	private String url;// 上一次请求的地址
	private Map parameterMap;// 上一次请求的所有参数

	public PageBean() {
		super();
	}

	/**
	 * 对分页bean进行初始化
	 * 
	 * @param request
	 */
	public void setRequest(HttpServletRequest request) {
		// 公共参数
		this.setPage(request.getParameter("page"));
		this.setRows(request.getParameter("rows"));
		this.setPagination(request.getParameter("pagination"));

		// 请求地址和请求参数
		this.setUrl(request.getContextPath() + request.getServletPath());
		this.setParameterMap(request.getParameterMap());
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Map getParameterMap() {
		return parameterMap;
	}

	public void setParameterMap(Map parameterMap) {
		this.parameterMap = parameterMap;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setPage(String page) {
		if (null != page && !"".equals(page.trim())) {
			this.page = Integer.parseInt(page);
		}
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public void setRows(String rows) {
		if (null != rows && !"".equals(rows.trim())) {
			this.rows = Integer.parseInt(rows);
		}
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public void setTotal(String total) {
		this.total = Integer.parseInt(total);
	}

	public boolean isPagination() {
		return pagination;
	}

	public void setPagination(boolean pagination) {
		this.pagination = pagination;
	}

	public void setPagination(String pagination) {
		if ("false".equals(pagination)) {
			this.pagination = false;
		}
	}

	/**
	 * 下一页
	 * 
	 * @return
	 */
	public int getNextPage() {
		int nextPage = page + 1;
		if (nextPage > this.getMaxPage()) {
			nextPage = this.getMaxPage();
		}
		return nextPage;
	}

	/**
	 * 上一页
	 * 
	 * @return
	 */
	public int getPreviousPage() {
		int previousPage = page - 1;
		if (previousPage < 1) {
			previousPage = 1;
		}
		return previousPage;
	}

	/**
	 * 最大页码
	 * 
	 * @return
	 */
	public int getMaxPage() {
		return total % rows == 0 ? total / rows : total / rows + 1;
	}

	/**
	 * 起始记录的下标
	 * 
	 * @return
	 */
	public int getStartIndex() {
		return (page - 1) * rows;
	}

	@Override
	public String toString() {
		return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]";
	}

}

你可能感兴趣的:(Spring)