SpringMVC對ServletAPI的支援和JSON格式的轉換

來源:互聯網
上載者:User

標籤:支援   實體   row   class   ring   exception   servlet   get   res   

package com.hongcong.controller;import java.io.UnsupportedEncodingException;import java.net.URLDecoder;import java.net.URLEncoder;import java.util.ArrayList;import java.util.List;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import com.hongcong.model.UserModel;@Controller@RequestMapping("/user")public class LoginController {    @RequestMapping("/list")    public ModelAndView list(){        ModelAndView modelAndView = new ModelAndView();        modelAndView.setViewName("/login/list");        return modelAndView;    }        @RequestMapping("/login")
//方法的參數可以直接擷取HttpServletRequest和HttpServletResponse
public String login(HttpServletRequest request,HttpServletResponse response)throws Exception{ String userName = request.getParameter("userName"); String passWord = request.getParameter("passWord"); //cookie中放入中文時需要進行特殊處理 String cookieStr = userName + "-" + passWord; String encode = URLEncoder.encode(cookieStr, "UTF-8"); Cookie cookie = new Cookie("user",encode); cookie.setMaxAge(60*30); response.addCookie(cookie); HttpSession session = request.getSession(); session.setAttribute("userModel", new UserModel(userName,passWord)); return "redirect:/user/list.do"; } @RequestMapping("/Ajax")
   //實體類轉換為json格式的註解,springMVC.xml需要配置 @ResponseBody
public List<UserModel> JsonTest(){ List<UserModel> list = new ArrayList<UserModel>(); list.add(new UserModel("小夏","1234")); list.add(new UserModel("小紅","111111")); return list; }}

 

SpringMVC對ServletAPI的支援和JSON格式的轉換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.