3将前台传来的参数 封装成map,后台接受

package com.air.front.web.controller;


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

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.air.common.pojo.user.contents.Consts;
import com.air.common.pojo.user.utils.IpUtils;
import com.air.common.pojo.user.utils.NumberUtils;
import com.alibaba.fastjson.JSONObject;


/**
 *
 * 功能描述: 接收参数
 * @author DHY
 * @Date 2018年6月2日
 */
@Component
public class BaseController {

    protected final static Logger logger = LoggerFactory.getLogger(BaseController.class);

	public String getNo(){
		String  NO= 	NumberUtils.getInstance().getOrderSn();
		return NO;
	}
	
	public static void main(String[] args) {
		String orderSn = NumberUtils.getInstance().getOrderSn();
		System.out.print(orderSn+"=======sss");
	}

    public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}

	public String getIpAddress()throws Exception{
		return IpUtils.getIpAddr(this.getRequest());
	}

    @SuppressWarnings("unchecked")
	public  Map<String, Object>  getParams() {
		HttpServletRequest request = getRequest();
		Map properties = request.getParameterMap();
		Object data = request.getAttribute(Consts.TOKEN_DATA);
		Map returnMap = new HashMap<String, Object>();
		Iterator entries = properties.entrySet().iterator();
		Map.Entry entry;
		String name = "";
		String value = "";
		while (entries.hasNext()) {
			entry = (Map.Entry) entries.next();
			name = (String) entry.getKey();
			Object valueObj = entry.getValue();
			if (null == valueObj) {
				value = "";
			} else if (valueObj instanceof String[]) {
				String[] values = (String[]) valueObj;
				for (int i = 0; i < values.length; i++) {
					value = values[i] + ",";
				}
				value = value.substring(0, value.length() - 1);
			} else {
				value = valueObj.toString();
			}
			returnMap.put(name, value);
		}
		logger.info(request.getServletPath()+"接口访问参数:"+JSONObject.toJSONString(returnMap));
		returnMap.put(Consts.TOKEN_DATA,data);
		return returnMap;
	}
}

 

使用方法: 

 public Object appUserLogin(){

        try {
            Map<String, Object> params = this.getParams();
            params.put("ip",getIpAddress());
            Map loginMap = userClient.appUserLogin(params);
            return loginMap;
        }catch (Exception e){
            logger.error("登陆失败!",e);
            return ResultUtil.repError("登陆失败!");
        }
    }

 

=============================================================

或者不用,直接用@requestBody, @RequestParam 

 public Object UserInfo( @RequestParam(value = "userNo")String userNo) {

        try {
            Map userMap = userClient.getUserInfo(userNo);
            return userMap;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取失败!",e);
        }

 

 

==========================================================================

1重定向和请求转发的区别

 

2servlet 宠写doGet()方法

3 servlet和springmvc的区别

Servlet:性能最好,处理Http请求的标准。

SpringMVC:开发效率高(好多共性的东西都封装好了,是对Servlet的封装,核心的DispatcherServlet最终继承自HttpServlet)

这两者的关系,就如同MyBatis和JDBC,一个性能好,一个开发效率高,是对另一个的封装。

 

(jsp的本质 是Servlet。我们访问一个jsp,并不是jsp直接运行的结果。而是服务器软件先把jsp翻译成Servlet,执行的是Servlet 的代码。 当我们第一次访问一个jsp的时候,服务器会把jsp文件翻译成java文件(Servlet),然后编译成class文件,最后执行的是这 个class文件。)

=============================================================

springmvc 的传递的参数

1 现在的注解 都对httpservletrequest进行了封装 比如 现在直接使用requestparam  body等 ,

2 public Object order(HttpServletRequest request, HttpServletResponse response, 
            @ModelAttribute OrderPo order)

HttpServletRequest request,HttpServletResponse response什么时候用到,比如,最常用到的  就是上传文件,传递字节流的时候  需要用到,或者从表头取东西等
=====================================================================

两行解决跨域问题

public Object order(HttpServletRequest request, HttpServletResponse response, 
            @ModelAttribute OrderPo order){


            response.setHeader("Access-Control-Allow-Origin", "*");

            response.setHeader("Access-Control-Allow-Method", "POST,GET");

}

 


 

阅读更多
换一批

没有更多推荐了,返回首页