springMVC II
来源:互联网 发布:windows安装mysql服务 编辑:程序博客网 时间:2024/06/04 19:46
package com.liyang;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.lang.annotation.Annotation;import java.util.Date;import java.util.HashMap;import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.codehaus.jackson.map.ObjectMapper;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.ui.ModelMap;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.portlet.bind.annotation.RenderMapping;import org.springframework.web.servlet.ModelAndView;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.sun.corba.se.spi.orbutil.fsm.Input;import com.sun.org.apache.regexp.internal.recompile;import com.sun.xml.internal.ws.util.StreamUtils;@Controllerpublic class HelloContorller implements Controller{ @RequestMapping(value="miss.do") @ResponseBody public ModelAndView handleRequest(HttpServletRequest request) throws Exception{ ModelAndView mav = new ModelAndView("messge"); mav.addObject("miss", "I miss you!" ); Map<String , Object> param = ProcessHttpParamsToMap(request) ; System.out.println("who=" + param.get("who")) ; mav.addObject("who", param.get("who") ); return mav; }//跳到页面 @RequestMapping(value="say.do") @ResponseBody public ModelMap say(HttpServletRequest request){ ModelMap mod = new ModelMap() ; mod.put("a", "_a") ; mod.put("z", "_z") ; mod.put("c", "_c") ; mod.put("b", "_b") ; return mod ; } // 返回的是json格式 @RequestMapping(value="ask.do", produces="text/html;charset=UTF-8") @ResponseBody public String ask(HttpServletRequest request) throws IOException{ Map<String , Object> param = ProcessHttpParamsToMap(request) ; try{ }catch(Exception e){ e.printStackTrace() ; } return "something int [Ask] is error!" ; } @RequestMapping(value="hi.do", produces="text/html;charset=UTF-8") @ResponseBody public String hi(HttpServletRequest request) throws IOException{ Map<String , Object> param = ProcessJosnParamsToMap(request) ; try{ return JSONObject.toJSONString(param) ; }catch(Exception e){ e.printStackTrace() ; } return "something int [Hi] is error!" ; } public static byte[] readStreamToBytes(InputStream inputStream) throws IOException{ ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inputStream.read(buffer)) != -1) { out.write(buffer, 0, len); } out.close(); inputStream.close(); return out.toByteArray(); } private Map<String, Object> ProcessHttpParamsToMap(HttpServletRequest request){ Map<String, String[]> params = request.getParameterMap() ; Map<String , Object> datas = new HashMap<String , Object>() ; if (params == null || params.isEmpty()) { return datas; } for(Map.Entry<String , String[]> e : params.entrySet()){ if(e.getKey().length() > 0) datas.put(e.getKey(), e.getValue()[0].trim()) ; } return datas; } private Map<String , Object> ProcessJosnParamsToMap(HttpServletRequest request) throws IOException{ InputStream inputStream = request.getInputStream() ; String charest = request.getCharacterEncoding() ; System.out.println(charest) ; if(charest == null || charest.isEmpty()){ charest = "UTF-8" ; } String requestStr = new String(readStreamToBytes(inputStream) , charest) ; System.out.println(requestStr); ObjectMapper objectMapper = new ObjectMapper(); Map<String, Object> paramMap; paramMap = objectMapper.readValue(requestStr, Map.class); return paramMap ; } @Override public Class<? extends Annotation> annotationType() { // TODO Auto-generated method stub return null; } @Override public String value() { // TODO Auto-generated method stub return null; }}
messge.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="GB18030"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body> ${miss} ${who}</body></html>
0 0
- springMVC II
- ii
- ii
- ii
- II
- ii
- SpringMVC
- springmvc
- SpringMVC
- SpringMVC
- springMvc
- springMVC
- springmvc
- springMVC
- springMVC
- springmvc
- SpringMVC
- SpringMVC
- 海明校验码
- 高质量c++(有了malloc/free 为什么还要new/delete )
- Java多线程[4]:线程间通信
- android 获取应用包名并启动
- POJ1164
- springMVC II
- OL2中实现百度地图ABCD marker的效果
- 颗粒归仓-操作系统总结
- struts2 action result 的配置
- uva 11400 - Lighting System Design
- lcd 显示(三)playDemo
- UIView CALayer属性不显示错误 Property cannot be found in forward class object 'CALayer'
- jenkins----邮箱配置
- java中swap()的实现