java Action 请求封装(一)
程序员文章站
2022-07-14 12:02:16
...
该框架是用Spring MVC 3.0实现,首先定义一个Action类,然后把init()方法给封装,然后在把?以后的所有数据全部封装到Map集合,实现方法如下:
/*
*Action 请求参数封装,不区分get和post请求
*zhouguohui
*/
public class Action {
private HttpServletRequest request;
private HttpServletResponse response;
private ActionValues values; //参数封装到Map集合里面
private MultipartFile[] files; //多个文件上传的路径地址
/**
* 初始化Action
* @param request
* @param response
* @param model
* @param fils
*/
@ModelAttribute
public void init(HttpServletRequest request,HttpServletResponse response,@RequestParam(required=false) MultipartFile[] fils,Model model){
this.request=request;
this.response=response;
this.values=new ActionValues(request);
if(null!=fils&&fils.length>0){
this.files=fils;
}
model.addAttribute("values", values);
response.setContentType("text/html;charset=UTF-8");
}
/**
* 输出字符串
* @param msg
*/
protected void print(Object msg){
PrintWriter out=null;
try{
out=response.getWriter();
out.print(msg);
}catch(Exception e){
e.printStackTrace();
}finally{
out.close();
}
}
/**
* 以Json格式输出参数
* @param obj
*/
protected void printJSON(Object obj){
PrintWriter out=null;
try{
out=response.getWriter();
out.print(JsonUtils.toJsonStr(obj));
}catch(Exception e){
e.printStackTrace();
}finally{
out.close();
}
}
/**
* 以keyName为键名,以obj为键值,返回Json对象
* @param keyName
* @param obj
*/
protected void printJSON(String keyName,Object obj){
PrintWriter out=null;
try{
out=response.getWriter();
out.print(JsonUtils.toJsonStr(keyName, obj));
}catch(Exception e){
e.printStackTrace();
}finally{
out.close();
}
}
public HttpServletRequest getRequest() {
return request;
}
public HttpServletResponse getResponse() {
return response;
}
/**
* 得到前台参数对象
* @param page
* @return
*/
public ActionValues getValues() {
return values;
}
/**
* 得到前台参数对象
* @param page
* @return
*/
public ActionValues getValues(boolean page) {
if(!page){
values.offPage();
}
return values;
}
/**
* 得到上传文件
* @return
*/
public MultipartFile[] getFiles() {
return files;
}
上一篇: Kafka配置SASL安全认证
下一篇: Java—split函数