Created
September 21, 2014 12:16
-
-
Save fuzhengwei/e5a16dd6f93a27f4e043 to your computer and use it in GitHub Desktop.
springmvc + spring + mybatis + easyui 在这一套应用中,easyui界面的json特殊格式,要用springmvc返回。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 返回json格式为: | |
* {"total":"2","rows":[{"id":"142318","firstname":"kkk","lastname":"Popoviceww","phone":"l","email":"dfgd@te.com"},{"id":"142322","firstname":"Hello","lastname":"there","phone":"","email":""}]} | |
*/ | |
@RequestMapping("doGetEmpList") | |
@ResponseBody | |
public Map<String, Object> doGetEmpList(){ | |
List<DrdgEmp> modelList = drdgEmpService.selectModelList(); | |
//获得的list集合装填到map集合 | |
Map<String, Object> map = new HashMap<String, Object>(); | |
//同时把total状态上 | |
map.put("total", String.valueOf(modelList.size())); | |
map.put("rows", modelList); | |
//返回的map结合会自动转换为json格式 | |
return map; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//现在对注解进行配置【详细配置】 | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpSession; | |
import org.apache.log4j.Logger; | |
import org.springframework.http.HttpEntity; | |
import org.springframework.http.HttpHeaders; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.ui.ModelMap; | |
import org.springframework.web.bind.ServletRequestBindingException; | |
import org.springframework.web.bind.ServletRequestUtils; | |
import org.springframework.web.bind.annotation.RequestBody; | |
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.servlet.ModelAndView; | |
import com.wy.pojo.User; | |
/** | |
* SpringMVC 注解 内置支持 | |
* @author Administrator | |
* | |
*/ | |
@Controller | |
@RequestMapping("/entryOrJsonController/") | |
public class EntryOrJsonController { | |
private Logger log = Logger.getLogger(EntryOrJsonController.class); | |
/** | |
* 输入为JSON格式的数据的方式 | |
* 1、@RequestBody 将httpRequest的body绑定到方法参数上 | |
* @param param | |
* @return | |
*/ | |
@RequestMapping(value = "annotationParam", method = RequestMethod.POST) | |
public ModelAndView annotationParam(@RequestBody User user){ | |
ModelAndView mav = new ModelAndView(); | |
log.info("============"+user); | |
mav.setViewName("welcome"); | |
return mav; | |
} | |
/** | |
* 输出为JSON格式的数据的方式 | |
* 1、@ResponseBody的作用是把返回值直接写到HTTP response body里 | |
* @param session | |
* @return | |
* @throws ServletRequestBindingException | |
*/ | |
@RequestMapping(value = "commonReturnType" , method = RequestMethod.GET) | |
@ResponseBody | |
public ModelAndView commonReturnType(HttpSession session) throws ServletRequestBindingException{ | |
ModelAndView mav = new ModelAndView(); | |
session.setAttribute("userName", "使用ResponseBody输出!"); | |
log.info("=================使用ResponseBody输出===================="); | |
ModelMap modelMap = new ModelMap(); | |
modelMap.put("mapKey", "mapValue"); | |
modelMap.addAttribute("attributeKey", "attributeValue"); | |
mav.addObject("model", modelMap); | |
mav.addObject("modelMap", modelMap); | |
mav.setViewName("welcome"); | |
return mav; | |
} | |
/** | |
* 输出为JSON格式的数据的方式 | |
* 2、ResponseEntity<?> | |
* @return | |
*/ | |
@RequestMapping(value = "annotationReturnType" , method = RequestMethod.GET) | |
public ResponseEntity<String> annotationReturnType(){ | |
log.info("=================使用ResponseEntity<?>输出===================="); | |
HttpHeaders httpHeaders = new HttpHeaders(); | |
ResponseEntity<String> responseEntity = new ResponseEntity<String>("Hello WY!", httpHeaders, HttpStatus.CREATED); | |
return responseEntity; | |
} | |
/** | |
* 输入 和输出为JSON格式的数据的方式 | |
* 3、HttpEntity<?> ResponseEntity<?> | |
* @param u | |
* @return | |
*/ | |
@RequestMapping(value = "annotationParamsReturn", method = RequestMethod.GET) | |
@ResponseBody | |
public ResponseEntity<String> annotationParamsReturn(HttpEntity<User> user){ | |
String temp = user.getBody().getUsername(); | |
ResponseEntity<String> responseResult = new ResponseEntity<String>(temp, HttpStatus.OK); | |
return responseResult; | |
} | |
/** | |
* | |
* @param request | |
* @param session | |
* @return | |
* @throws ServletRequestBindingException | |
*/ | |
@RequestMapping(value = "login" , method = RequestMethod.GET) | |
public ModelAndView login(HttpServletRequest request, HttpSession session) throws ServletRequestBindingException{ | |
ModelAndView mav = new ModelAndView(); | |
String userName = request.getParameter("userName"); | |
String password = ServletRequestUtils.getStringParameter(request, "password"); | |
log.info("============================\r\n接收到的参数: "+userName+" "+password); | |
session.setAttribute("userName", userName); | |
mav.setViewName("welcome"); | |
return mav; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
首先要引入相应jar包 | |
jackson-core-asl.jar | |
jackson-mapper-as.jar | |
--> | |
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" | |
xsi:schemaLocation="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 | |
http://www.springframework.org/schema/mvc | |
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> | |
<!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 对于使用根据自己的配置来用--> | |
<context:component-scan base-package="com.drdg.*.controller" /> | |
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> | |
<bean | |
class="org.springframework.web.servlet.view.InternalResourceViewResolver" | |
p:prefix="/" p:suffix=".jsp" /> | |
<!-- 处理在类级别上的@RequestMapping注解--> | |
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> | |
<!-- 处理方法级别上的@RequestMapping注解--> | |
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > | |
<property name="messageConverters" ref="mappingJacksonHttpMessageConverter"></property> | |
</bean> | |
<!-- 处理JSON数据转换的 --> | |
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> | |
<!-- 为了处理返回的JSON数据的编码,默认是ISO-88859-1的,这里把它设置为UTF-8,解决有乱码的情况 --> | |
<property name="supportedMediaTypes"> | |
<list> | |
<value>text/html;charset=UTF-8</value> | |
</list> | |
</property> | |
</bean> | |
</beans> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment