package com.atguigu.controller;
import com.atguigu.entity.Role;
import com.atguigu.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Controller
@RequestMapping(value = "/role")
@SuppressWarnings({"unchecked", "rawtypes"})
public class RoleController {
@Autowired
private RoleService roleService;
@RequestMapping("/index")
public String index(ModelMap model) {
List<Role> list = roleService.findAll();
model.addAttribute("list", list);
return "role/index";
}
@RequestMapping("/create")
public String create() {
return "role/create";
}
@RequestMapping("/save")
public String save(Role role, HttpServletRequest request) {
roleService.insert(role);
return "common/successPage";
}
@RequestMapping("/delete/{role_id}")
public String delete(@PathVariable Long role_id) {
roleService.delete(role_id);
return "redirect:/role/index";
}
@RequestMapping("/edit/{role_id}")
public ModelAndView edit(@PathVariable Long role_id) {
//根据id查询对象
Role role = roleService.getRoleById(role_id);
ModelAndView mv = new ModelAndView();
mv.addObject("role",role);
mv.setViewName("role/update");
return mv;
}
@RequestMapping("/update")
public String update(Role role) {
roleService.update(role);
return "common/successPage";
}
}
可以发现转发的地址都没有/开头, 重定向的地址是以/开头的
我是个初学者,后来想明白了, 转发的地址, 会通过thymeleaf解析器进行前缀和后缀拼接,填写的是页面的路径"role/update"就是role文件夹下的update.html
重定向,相当于在浏览器地址栏直接输入的地址,这个地址是spring @Controller的地址,通过@Controller 返回一个页面地址,再通过thymeleaf处理,才能找到页面