Leopard Web -- 页面特殊参数

     什么是页面特殊参数?

     在Leopard中,定义的页面特殊参数就是在controller中只需要写上这个参数的名字,就可以在方法中拿到值 。比如:sessUserId, 就是从session中获取userId的值。

     Leopard中定义了很多页面特殊参数,包括:

     proxyIp:获取离服务器最近的机器IP

     requestUri:当前请求的URI

     sessCaptcha:session中的验证码

     sessionId:获取sessionId

     sessUserId:当前登录的用户ID

     sessUsername:当前登录的用户名

     userAgent:获取浏览器类型(User-Agent)

     这些参数被定义为Leopard页面特殊参数,或者称之为关键字参数。以上页面特殊参数,就像java的关键字一样。在页面传递的时候,如果出现同名的参数,将被忽略页面参数值。举个例子:比如你在页面提交时有一个属性名为sessUserId,被赋值为1,而session中的sessUserId是2,那么在controller中全拿到的值是2,而不是1。1将被忽略,因此在页面传递时,请不要使用以上页面特殊参数。

     

      通过上以上的描述,相信各位同学对页面特殊参数已经有了一定的了解。

      对于页面特殊参数,那就是一个字“屌炸天”!对不起,请宽恕我,我是受坐山雕的影响!

      在web系统中,难免会将一些会话中常用的值存入Session中,比如说:当前登录用户的机构信息、当前登录用户的角色信息、当前登录用户的用户信息等等。

      当需要从Session中获取值时,通常会采取在Controller 的方法入口参数传入HttpServletRequest ,然后再调用request.getSession().getAttribute("xxxx")来获取值。严谨的写法,还需要判断requst.getSession、返回值等是否为空?

      例如:在session中获取userId的值,通常的写法如下:

     

package io.leopard.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class SessionUtil {

	public static String getSessUserId(HttpServletRequest request) {
		String sessUserId = "";
		HttpSession session = request.getSession();
		if (session != null) {
			sessUserId = (String) session.getAttribute("sessUserId");
		}
		return sessUserId;
	}

}

   

    那么在Controller里获取sessUserId如下:

    

package io.leopard.web.mvc.controller;

import io.leopard.util.SessionUtil;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserController {

	@RequestMapping(value = "/checkMobile.do")
	public ModelAndView checkMobile(HttpServletRequest request)
			throws IOException {
		ModelAndView model = new ModelAndView("/web/user/checkMobile");
		String userId = SessionUtil.getSessUserId(request);
		return model;
	}
}

   

     以上方法,有二点让我难以接受:

     1、在checkMobile方法里传入了HttpServletRequest这个参数,因为HttpServletRequest是一个包装对象,编写测试代码时需要参照源码进行参数值设置。

   2、每次获取userId都需要写一行代码:SessionUtil.getSessUserId(request);

 

    原始的写法我们已经见识,那么在Leopard中的写法是怎么样的呢?只需要在方法入口中传入sessUserId这个参数即可,代码如下:

package io.leopard.web.mvc.controller;

import java.io.IOException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserController {

	@RequestMapping(value = "/checkMobile.do")
	public ModelAndView checkMobile(String sessUserId) throws IOException {
		ModelAndView model = new ModelAndView("/web/user/checkMobile");
		return model;
	}
}

  

  真是应了那一个字-“屌炸天”,那么sessUserId是怎么获取到对应的值的呢?只需要简单的二步即可以搞定:

  1、实现接口PageParameter中的二个方法:getKey(), getValue(HttpServletRequest request); 

  2、注册实现类为bean,使用注解的形式。对了,别忘记在xml中进行扫描。 

  

  上例中sessUserId的实现代码如下:

  

package io.leopard.web4j.parameter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.NotImplementedException;
import org.springframework.stereotype.Service;

/**
 * 获取当前登录的自定义用户Id
 * 
 * @author 阿海
 * 
 */
@Service
public class SessUserIdPageParameter implements PageParameter {

	@Override
	public String getValue(HttpServletRequest request) {
		String sessUserId = "";
		HttpSession session = request.getSession();
		if (session != null) {
			sessUserId = (String) session.getAttribute("sessUserId");
		}
		return sessUserId;
	}

	@Override
	public String getKey() {
		return "sessUserId";
	}

}

  

  Leopard不建议在session中存入过多的参数,尤其是对象参数。存入过多参数,在开启分布式时,有可能会导致情能问题!

   

    

猜你喜欢

转载自wsppkdc.iteye.com/blog/2182533
今日推荐