Control的方法中return不跳轉頁面

返回的默認爲resource/templetes下的html或jsp文件
 

1.Control包中的類若是加入的註解爲@Controller
「使用@Controller註解,在對應方法上視圖解析切能夠解析return的jsp\html頁面,而且跳轉到相對應的頁面」
 
@Controller
public class UnitConntrol {
    @Autowired
    UnitService unitService;
    @RequestMapping(value = "/findUintId/{id}",method = RequestMethod.GET)
    public Object findUnitId(@PathVariable("id") Integer id, Model model){
        System.out.println("jinru");
        Unit unit = unitService.selectByPrimaryKey(id);
        System.out.println(unit.getName());
        model.addAttribute(unit);
        return "unitview";
    }
/**
——第二種方法---
@RequestMapping(value = "/findUintId/{id}",method = RequestMethod.GET)
public @ResponseBody ModelAndView findUnitId(@PathVariable("id") Integer id, Model model){
    System.out.println("jinru");
    Unit unit = unitService.selectByPrimaryKey(id);
    System.out.println(unit.getName());
    model.addAttribute(unit);
    return new ModelAndView("unitview.html");
}
*/
}

2.Control包中的類若是加入的註解爲@RestController{RestController = ResponseBody + Controller}html

方法return沒法返回jsp/html頁面,配置的視圖解析器 InternalResourceViewResolver不起做用,返回的內容就是Return裏的內容。
 
@RestController
public class UserControl {
    @Autowired
    UserService userService;
//根據ID對user查看
    @RequestMapping(value = "/findByUserid/{id}",method = RequestMethod.GET)
    public ModelAndView findByUserid(@PathVariable("id")Integer id, Model model){
        System.out.println(「running findByUserid");
        User user =  userService.findByUserid(id);
        System.out.println(user.getName());
        model.addAttribute("user",user);
        return new ModelAndView("userview.html");
    }
}
相關文章
相關標籤/搜索