SSM 整合的坑

页面显示原本的字符

  • jsp的 isELIgnored="false"
     

路径问题总结

  • 在index.jsp的表单提交是 <form action="account/save" method="post">
  • 如果写成了<form action="/account/save" method="post"> 会跳转http://localhost:8888/ssm_war/account/account/findAll 错误

 

  • 下图中/account/findAll是正确的,如果写成account/findAll,地址栏还是http://localhost:8888/ssm_war/account/save,但是404报错,没有路径提示,小心
@Controller
@RequestMapping("/account")
public class AccountController {
    @RequestMapping("/findAll")
    public String findAll(Model model){
    ···    
    }
    @RequestMapping("/save")
    public void saveAccount(Account account, HttpServletRequest res, HttpServletResponse rsp) throws ServletException, IOException {
        accountService.saveAccount(account);
        res.getRequestDispatcher("/account/findAll").forward(res, rsp);
    }
}

 

  • sendRedirct方法中/account/findAll会跳转http://localhost:8888/account/findAll,
  • sendRedirct方法中account/findAll会跳转http://localhost:8888/ssm_war/account/account/findAll
  • res.getContextPath()表示的是/ssm_war,后面没有斜杠
@Controller
@RequestMapping("/account")
public class AccountController {
    @RequestMapping("/findAll")
    public String findAll(Model model){
        ···    
    }
    @RequestMapping("/save")
    public void saveAccount(Account account, HttpServletRequest res, HttpServletResponse rsp) throws ServletException, IOException {
        accountService.saveAccount(account);
        //都错rsp.sendRedirect("/account/findAll");
        //下面才对 
        rsp.sendRedirect(res.getContextPath()+"/account/findAll"); 
    }
}

 

当然,都可以用springmvc的return "forward:xxxxx和return "redirect:xxxx简化
 

  • 下面正确跳转,"forward:转发的JSP路径",不走视图解析器了,所以需要编写完整的路径
  • 如果写成了WEB-INF/pages/list.jsp会报/ssm_war/account/WEB-INF/pages/list.jsp 404错误
@Controller
@RequestMapping("/account")
public class AccountController {
    @RequestMapping("/findAll")
    public String findAll(Model model){
        ···    
    }
    @RequestMapping("/save")
    public String saveAccount(Account account) throws ServletException, IOException {
        return "forward:/WEB-INF/pages/list.jsp";
    }
}

 

  • 下面也正确
  • 如果写成了account/findAll 跳转到http://localhost:8888/ssm_war/account/save,报404错误,没有路径提示,小心
@Controller
@RequestMapping("/account")
public class AccountController {
    @RequestMapping("/findAll")
    public String findAll(Model model){
        ···    
    }
    @RequestMapping("/save")
    public String saveAccount(Account account) throws ServletException, IOException {
        return "forward:/account/findAll";
    }
}

 

  • 下面的正确,会跳转到http://localhost:8888/ssm_war/index.jsp
  • springmvc的重定向 不用加项目名称
  • 如果写成redirect:index.jsp,会跳转到http://localhost:8888/ssm_war/account/index.jsp,不正确
@Controller
@RequestMapping("/account")
public class AccountController {
    @RequestMapping("/findAll")
    public String findAll(Model model){
        ···    
    }
    @RequestMapping("/save")
    public String saveAccount(Account account) throws ServletException, IOException {
        //return "redirect:/index.jsp";
    }
}

 

  • 下面的正确,跳转到http://localhost:8888/ssm_war/account/findAll
  • 如果写成"redirect:account/findAll"会跳转到http://localhost:8888/ssm_war/account/account/findAll
@Controller
@RequestMapping("/account")
public class AccountController {
    @RequestMapping("/findAll")
    public String findAll(Model model){
        ···    
    }
    @RequestMapping("/save")
    public String saveAccount(Account account) throws ServletException, IOException {
        return "redirect:/account/findAll";
    }
}
posted @ 2020-04-29 14:58  种树人  阅读(243)  评论(0编辑  收藏  举报