ModelAndView
程序员文章站
2022-07-03 12:06:00
...
package controller;
import model.Address;
import model.User;
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.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping(value = "/user")
public class AnnoController {
@RequestMapping("/dobiz.do")
public ModelAndView doBiz(){
ModelAndView mv = new ModelAndView("index");
mv.addObject("mess","hello world1");
return mv;
}
@RequestMapping("/dobiz2.do")
public ModelAndView doBiz2(){
ModelAndView mv = new ModelAndView("index");
mv.addObject("mess","hello world2");
return mv;
}
/**
* 返回servlet编程
*/
@RequestMapping("/dobiz3.do")
public void doBiz3(HttpServletRequest request, HttpServletResponse response) throws Exception{
String mess = "hello world3";
request.setAttribute("mess",mess);
request.getRequestDispatcher("/WEB-INF/jsp/index2.jsp").forward(request,response);
}
/**
* 隐式转发
* @param request
* @return
*/
@RequestMapping("/dobiz4.do")
public String doBiz4(HttpServletRequest request){
String mess = "hello world4";
request.setAttribute("mess",mess);
return "index";//逻辑视图名
}
/**
* 隐式转发
* @param model
* @return
*/
@RequestMapping("/dobiz5.do")
public String doBiz5(Model model){
String mess = "hello world5";
//封装数据
model.addAttribute("mess",mess);
return "index";//逻辑视图名
}
/**
* 显式转发,忽略视图解析器
* @param model
* @return
*/
@RequestMapping("/dobiz6.do")
public String doBiz6(Model model){
String mess = "hello world6";
//封装数据
model.addAttribute("mess",mess);
return "forward:/WEB-INF/jsp/index.jsp";
}
/**
* 显式重定向,忽略视图解析器
* @param model
* @return
*/
@RequestMapping("/dobiz7.do")
public String doBiz7(Model model){
String mess = "hello world7";
//封装数据
model.addAttribute("mess",mess);
return "redirect:/user/dobiz6.do";
}
@RequestMapping("/dobiz8.do")
public String doBiz8(Map<String,Object> map){
String mess = "hello world8";
map.put("mess",mess);
return "index";
}
@RequestMapping("/dobiz9.do")
public String doBiz9(ModelMap map){
String mess = "hello world9";
// map.put("mess",mess);
map.addAttribute("mess",mess);
return "index";
}
@RequestMapping("/dobiz10.do")
public ModelAndView doBiz10(ModelAndView mv){
String mess = "hello world10";
mv.setViewName("index");
mv.addObject("mess",mess);
return mv;
}
@RequestMapping("/dobiz11.do")
public String doBiz11(int id,String username,String password){
System.out.println(id+","+username+","+password);
return "index";
}
/**
* 普通POJO传参
* @param user
* @param username
* @return
*/
@RequestMapping("/dobiz12.do")
public String doBiz12(User user,String username){
System.out.println(user.getId()+","+user.getName()+","+user.getPassword());
System.out.println(username);
return "index";
}
/**
* 复杂POJO的传参
* @param user
* @param
* @return
*/
@RequestMapping("/dobiz13.do")
public String doBiz13(User user){
System.out.println(user.getId()+","+user.getName()+","+user.getPassword());
System.out.println(user.getAddr().getName());
return "index";
}
/**
* 集合类型参数绑定
* @return
*/
@RequestMapping("/dobiz14.do")
public String doBiz14(User user){
System.out.println(user.getList());
if(user.getList()!=null){
for(Address addr:user.getList()){
System.out.println(addr.getName());
}
}
System.out.println("---------------------------");
System.out.println(user.getMap());
return "index";
}
@RequestMapping("/toform.do")
public String toForm(){
return "form";
}
/**
* 绑定日期对像
* @param
* @return
*/
@RequestMapping("/dobiz15.do")
public String doBiz15(User user){
System.out.println(user.getDate());
return "index";
}
}
上一篇: 不同的搜索引擎 SEO方面有什么不同?
推荐阅读