SpringMVC

jar包

commons-logging-1.1.3.jar
fastjson-1.1.26-1.1.26.jar
jackson-all-1.9.11-1.9.11.jar
jstl.jar
spring-aop-4.0.4.RELEASE.jar
spring-aspects-4.0.4.RELEASE.jar
spring-beans-4.0.4.RELEASE.jar
spring-context-4.0.4.RELEASE.jar
spring-context-support-4.0.4.RELEASE.jar
spring-core-4.0.4.RELEASE.jar
spring-expression-4.0.4.RELEASE.jar
spring-framework-bom-4.0.4.RELEASE.jar
spring-instrument-4.0.4.RELEASE.jar
spring-instrument-tomcat-4.0.4.RELEASE.jar
spring-jdbc-4.0.4.RELEASE.jar
spring-jms-4.0.4.RELEASE.jar
spring-messaging-4.0.4.RELEASE.jar
spring-orm-4.0.4.RELEASE.jar
spring-oxm-4.0.4.RELEASE.jar
spring-test-4.0.4.RELEASE.jar
spring-tx-4.0.4.RELEASE.jar
spring-web-4.0.4.RELEASE.jar
spring-webmvc-4.0.4.RELEASE.jar
spring-webmvc-portlet-4.0.4.RELEASE.jar
spring-websocket-4.0.4.RELEASE.jar
standard.jar

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

  <servlet>  
         <servlet-name>dispatcherServlet</servlet-name>  
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
         <load-on-startup>1</load-on-startup>  
   </servlet>  

   <servlet-mapping>  
         <servlet-name>dispatcherServlet</servlet-name>  
         <url-pattern>/</url-pattern>  
   </servlet-mapping>  


</web-app>

dispatcherServlet-servlet.xml

<?xml version="1.0" encoding= "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <mvc:annotation-driven/>
  <mvc:default-servlet-handler/>

  <context:component-scan base-package="com.liyang"/>    
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
     <property name="prefix" value="/WEB-INF/"></property>
     <property name="suffix" value=".jsp"></property>
   </bean>      
</beans>
package com.liyang;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
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 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;


@Controller
public class HelloContorller {

       @RequestMapping(value = "say")
       public ModelMap say(HttpServletRequest request){
              HashMap<String, String[]> pa = (HashMap<String, String[]>) request.getParameterMap() ;
              System.out.println("gao" + System.currentTimeMillis())   ;
              ModelMap md = new ModelMap() ;
              md.addAttribute("name", pa.get("name")[0]) ;
              md.addAttribute("age", pa.get("age")[0]) ;
              return md ; 
       }


       @RequestMapping(value="ask", produces="text/html;charset=UTF-8")
       @ResponseBody
       public String ask(HttpServletRequest request) throws IOException{
              Map<String , Object> param = ProcessHttpParamsToMap(request) ;
              try{
//                JSONObject js = new JSONObject() ;
//                js.put("name", param.get("name").toString() ) ;
//                js.put("age", param.get("age").toString() )  ;
                  return JSONObject.toJSONString(param) ;
              }catch(Exception e){
                  e.printStackTrace() ; 
              }
              return "something int [Ask] is error!" ;
       }

       @RequestMapping(value="hi", produces="text/html;charset=UTF-8")
       @ResponseBody
       public String hi(HttpServletRequest request) throws IOException{
              Map<String , Object> param = ProcessJosnParamsToMap(request) ;
              try{
//                JSONObject js = new JSONObject() ;
//                js.put("name", param.get("name").toString() ) ;
//                js.put("age", param.get("age").toString() )  ;
                  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 ; 
        }

}

你可能感兴趣的:(SpringMVC)