zoukankan      html  css  js  c++  java
  • 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";
        }
    }
    












    种一棵树最好的时间是十年前,其次是现在。
  • 相关阅读:
    js高级程序设计 笔记 --- 引用类型
    es6 简单封装一个 省市县三级下拉框
    js中元素、触点等各种距离的总结
    css实现视觉差的滚动
    js的节流和防抖
    js关于原型,原型链的面试题
    深入理解promise
    vue 同一个组件的跳转, 返回时保留原来的下拉位置
    es6 封装一个登录注册的验证滑块
    洛谷P3203 [HNOI2010]弹飞绵羊(lct)
  • 原文地址:https://www.cnblogs.com/islch/p/12802219.html
Copyright © 2011-2022 走看看