【问题标题】:How to pass JSP c:set variable in Spring Controller如何在 Spring Controller 中传递 JSP c:set 变量
【发布时间】:2014-09-08 10:46:32
【问题描述】:

我在我的 jsp 页面中声明了一个简单的变量

 <html>
   <body>
     <c:set var="vehicle" scope="request" value="Car" />
     <td><a href="<%=request.getContextPath()%>/productsHome/vehicles">Cars</a></td>
   </body>
 </html>

我正在尝试在我的 Spring 控制器中访问值为“Car”的变量车辆

  @RequestMapping(value = "/vehicles", method = RequestMethod.GET)
  public ModelAndView viewLaptops(@RequestParam(value = "vehicle", required = false) String    vehicleType) {

      if (vehicleType.equals("Car")) {
            // retrieve car list, return the model for car list
      }
      else if (vehicleType.equals("Truck")) {
            // retrieve truck list, return the model for truck list
      }
      System.out.println(carType);

  } 

但我得到一个空值。我怎样才能实现这一目标?感谢您的帮助。

【问题讨论】:

    标签: spring jsp spring-mvc controller


    【解决方案1】:

    尝试如下更新生成的url:

    <c:set var="vehiculeURL">  
      <c:url value="productsHome/vehicles">    
        <c:param name="vehicle" value="Car"/>      
      </c:url>    
    </set>  
    <a href="${vehiculeURL}">Car</a> 
    

    【讨论】:

    • 谢谢,但我的控制器中仍然有一个空值:(
    • 你能检查一下生成的 URL 吗?它看起来像什么?
    • localhost:8080/Spring.MVC/productsHome/vehicles ,我试图在我的控制器中打印值仍然为空,System.out.println(vehicleType);
    【解决方案2】:

    要将值从弹簧控制器传递给您的 .jsp,您可以使用 Model 参数给您的函数:

    @RequestMapping(value = "/vehicles", method = RequestMethod.GET)
    public String viewLaptops(Model model, ...) {
        model.addAttribute("Car", "Toyota");
        return "index.jsp"; //path to your file
    }
    

    通过使用以下语法,您将在 jsp 中使用键“Car”获得值“Toyota”:

    <div>${Car}</div>
    

    【讨论】:

    • 感谢您提供示例,但我在问,如何将 jsp 变量值传递给 spring 控制器。我的错误,我应该把标题“如何将 JSP c:set 变量传递给 Spring Controller”而不是“in”,对不起我的英语不好。
    【解决方案3】:

    我设法解决了我想要发生的所有事情,使用@PathVariable,我可以在单个控制器中映射多个 url 请求,假设我有 3 个 href

    <td><a href="<%=request.getContextPath()%>/productsHome/vehicles/Car">Cars</a></td>
    <td><a href="<%=request.getContextPath()%>/productsHome/vehicles/Truck">Trucks</a></td>
    <td><a href="<%=request.getContextPath()%>/productsHome/vehicles/Bike">Bike</a></td>
    

    我在控制器类中添加了另一个 handler-method,它将支持我与 /vehicle 的默认映射

    @RequestMapping(value = "/vehicles", method = RequestMethod.GET)
    public ModelAndView viewVehicles() {
    
        ModelAndView mv = new ModelAndView();
        mv.setViewName("vehiclesPage");
    
        return mv;
    }
    
    @RequestMapping(value = "/vechicles/{type}", method = RequestMethod.GET)
    public ModelAndView viewDifferentVehicles(@PathVariable("type") String type) {
    
        ModelAndView mv = new ModelAndView();
    
        if(type.equals("Car") {
            mv.setViewName("cars"); // cars.jsp
        }
        else if (type.equals("Truck") {
            mv.setViewName("trucks"); // trucks.jsp
        }
        else if (type.equals("Bike") {
            mv.setViewName("bikes");  //bikes.jsp
        }
    
        return mv;
    }
    

    这正是我想要发生的事情,映射不同的请求并在单个处理程序方法中返回所需的视图,我认为使用一些 if-else 结构访问 JSP 变量将解决我的问题,只是我偶然发现的一些随机运气@PathVariable

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-23
      • 2018-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      相关资源
      最近更新 更多