1

我正在尝试使用spring框架在java中开发一个网络监控应用程序。它列出了网络中的所有资源。通过单击其中一个资源 ID,页面必须重定向到一个新页面,该页面显示该资源的所有详细信息。这样,下一个页面会根据我们点击的资源 id 更改为新内容。现在我只是通过在控制器中映射页面来连接下一页,但是页面是空的,即值没有正确传递给控制器​​。

我的控制器:

@RequestMapping(value = "/deviceInformation", method = RequestMethod.GET)
public String deviceinfo(ModelMap model, HttpServletRequest request) {
    List<NetworkInterface> list = inventoryService.getNetworkInterfaces("");
    List<NetworkService> list2 = inventoryService.getNetworkServices("ipResource.discoveredName");
    List<Equipment> list3 = inventoryService.getEquipments("resourceId");
    List<Processor> list4 = inventoryService.getProcessors("hostId");
    List<SoftwareRunning> list5 = inventoryService.getSoftwaresRunning("hostId");
    List<StorageDisk> list6 = inventoryService.getStorageDisks("hostId");
    List<StorageMemory> list7 = inventoryService.getStorageMemories("hostId");

    model.addAttribute("INlist", list);
    model.addAttribute("Serlist", list2);
    model.addAttribute("Eqlist", list3);
    model.addAttribute("Prolist", list4);
    model.addAttribute("SRlist", list5);
    model.addAttribute("Storlist", list6);
    model.addAttribute("StoMry", list7);

    return "inventory/deviceInformation";
}

@RequestMapping(value = "/interfaceList/{ipResource.discoveredName}", method = RequestMethod.POST)
public ModelAndView getInterface(@PathVariable String resourceId, HttpServletRequest request,
                                 HttpServletResponse response) {
    System.out.println("Got request param: " + resourceId);

    List<NetworkInterface> list = inventoryService.getNetworkInterfaces("resID");
    List<NetworkService> list2 = inventoryService.getNetworkServices("ipResource.discoveredName");
    List<Equipment> list3 = inventoryService.getEquipments("ipResource.discoveredName");
    List<Processor> list4 = inventoryService.getProcessors("hostId");
    List<SoftwareRunning> list5 = inventoryService.getSoftwaresRunning("hostId");
    List<StorageDisk> list6 = inventoryService.getStorageDisks("hostId");
    List<StorageMemory> list7 = inventoryService.getStorageMemories("hostId");
    ModelAndView modelAndView = new ModelAndView("/inventory/deviceInformation");
    modelAndView.addObject("INlist", list);
    modelAndView.addObject("Serlist", list2);
    modelAndView.addObject("Eqlist", list3);
    modelAndView.addObject("Prolist", list4);
    modelAndView.addObject("SRlist", list5);
    modelAndView.addObject("Storlist", list6);
    modelAndView.addObject("StoMry", list7);
    return modelAndView;
}
4

1 回答 1

1

这是一个示例,它有一个弹簧控制器和两个 jsp 文件。此示例仅用于帮助目的,并不意味着使用 Spring MVC 的最佳实践。控制器 :

    @RequestMapping(value="/getdevices")
    public ModelAndView generateTable(){
        ModelAndView mv = new ModelAndView("table");
        List<Device> devices = new ArrayList<Device>();
        devices.add(new Device(1,"Iphone5",500.00));
        devices.add(new Device(1,"Iphone4s",450.00));
        mv.addObject("devices", devices);
        return mv;
    }
    @RequestMapping(value="/getdeviceinfo")
    public ModelAndView getDeviceInfo(@RequestParam Integer id, @RequestParam String name,@RequestParam Double price){
        ModelAndView mv = new ModelAndView("device");
        Device device = new Device(id,name,price);
        mv.addObject("device", device);
        return mv;
    }

table.jsp(生成带有指向每个单独设备信息页面的链接的设备列表)

<ul>
<c:forEach var="device" items="${devices}">
    <li><a href="getdeviceinfo?id=${device.id}&name=${device.name}&price=${device.price}">${device.name}</a></li>
</c:forEach>
</ul>

device.jsp(单个设备的信息)

<body>
${device.name}
</body>

device.java(简单 pojo)

public Integer id;
    private String name;
    private Double price;

    public Device(Integer id, String name, Double price) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
    }

注意:我注意到在您的最新代码中,您已将视图设置为“/inventory/deviceInformation”。我不确定您的网络应用程序结构是什么样的。View 的值取决于您在 web 根目录下的结构化 jsp 文件的方式以及 spring 视图解析器的前缀。例如,在我的测试 spring web 应用程序中,我在 /webapp/WEB-INF/views 下有 table.jsp 和 device.jsp,spring config 就像

<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

如您所见,在上面的控制器中,视图中没有前导斜杠,例如 new ModelAndView("table.jsp)

于 2013-02-01T12:36:45.913 回答