Springmvc 接收参数,返回参数,时间转换,注解

目录

  • 一级目录
    • void
    • 时间转换器
    • 默认参数绑定
    • 重定向
    • 重定向的传值问题
    • 注解
      • @RequestMapping
      • @RequestParam
      • @SessionAttributes
      • @ModelAttribute
      • @RequestHeader
      • @RequestBody
      • @CookieValue
      • @PathVariable
    • Springmvc ant风格

一级目录

void

/*
  返回值类型为void : 无返回值的时候
  void
  情况一:
   404异常 找不到: /WEB-INF/views/void11.jsp
   他会将 @RequestMapping 当成返回值
   等同写成了返回值为String类型的简写
    public  String  show(){
      return "void11";
    }
    
  情况二:
   等同于返回值为任何类型
   可以将任何数据打包成json
 */
@Controller
public class LoginController  extends JsonController {
   
//    情况一
    @RequestMapping("void001")
    public void show(){
   
        System.out.println("---这是返回值为void。。。");
    }

//情况二
    /*
    JsonUtilObjecToJson(HttpServletResponse response,
			String code, String msg, Object object)
     */
     @RequestMapping("void002")
     public void void002(HttpServletResponse response){
   
//         String name = "xiaowang";
//         JsonUtilObjecToJson(response,"","",name);

         Person person = new Person();
         person.setPid(1001);
         person.setPname("laowang");
         person.setAddress("shandong");
         JsonUtilObjecToJson(response,"200","success",person);

     }
}

Json工具类
将任意object类型数据转换成json类型,并放进json域中

@Controller
public class JsonController {
   
    private String codeKey = "code";
    private String msgKey = "msg";
    private String dateKey = "date";
    private static final Logger LOGGER = LoggerFactory
            .getLogger(JsonController.class);


    protected void JsonUtilObjecToJson(HttpServletResponse response,
                                       String code, String msg, Object object) {
   
        response.setContentType("application/json;charset=UTF-8");
        ObjectMapper mapper = new ObjectMapper();
        final String resultCode = code;
        final String resultMsg = msg;
        final Object resultObject = object;
        try {
   
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            mapper.writeValue(response.getWriter(),
                    new HashMap<String, Object>() {
   
                        {
   
                            put(codeKey, resultCode);
                            put(msgKey, resultMsg);
                            put(dateKey, resultObject);
                        }
                    });
        } catch (JsonGenerationException | JsonMappingException e) {
   
            e.printStackTrace();
        } catch (IOException e) {
   
            e.

你可能感兴趣的:(Java第三阶段笔记集,java,spring,springmvc)