【问题标题】:Spring MVC with Hibernate Lists带有休眠列表的 Spring MVC
【发布时间】:2016-01-26 07:16:14
【问题描述】:

所以我在 Oracle 中设置了一些表,使用 Spring MVC,我能够获取每个表中所有对象的列表,并使用 spring 表单选择在 jsp 中显示它们。现在我似乎无法弄清楚如何允许用户选择一个,保留用户选择的一个,然后使用他们选择的 OBJECT 执行操作。我能够获得他们选择的字符串,但这无济于事,因为我需要选择的实际对象。

@Controller
public class TestController {

    @Autowired
    GenericDAO<Material> matDAO;

    @Autowired
    GenericDAO<Structure> structDAO;

    @RequestMapping(method=RequestMethod.GET)
    public ModelAndView getForm(){

        ModelAndView model = new ModelAndView("testForm");

        return model;

    }

    @ModelAttribute
    public void displayForm(Model model){
        List<Structure> structList = structDAO.retrieveAll(Structure.class);
        model.addAttribute("structure", new Structure());
        model.addAttribute("structList", structList);

    }

    @RequestMapping(value="/submitChoices.html", method=RequestMethod.POST)
    public ModelAndView getStruct(@ModelAttribute("structure") Structure struct){

        //Manipulate Structure Object Here

    } 


}

JSP 页面:

<body>

<springform:form method="POST" action="/project/submitchoices.html">

    <springform:select>
        <springform:option value="0" label="--- Select One ---"/>
        <springform:options items="${structList}" itemValue="type" itemLabel="type"/>

    </springform:select>

    <input type="submit" value="submit">
</springform:form>

</body>

【问题讨论】:

    标签: java hibernate jsp spring-mvc


    【解决方案1】:

    您需要每个选项的对象 ID,以便 Hibernate 可以识别每个选项。 ID 将您与选择的对象联系起来。现在你只有 structList。注意上面有一个值=“0”。在填充的每个实际选择中,它们也需要值。

    【讨论】:

    • 在我的结构类中,我有字段“type”和“sID”,其中“sID”是主键。我可以将上述 JSP 中的 itemValue 更改为“sID”吗?
    • 我会用 ${sID} 代替 type;然后当您回到控制器时,您可以使用 id 来检索和/或使用该对象,因为您知道它是哪一个。
    • 那行不通。我认为 $ 符号仅用于从控制器获取某些内容并使用来自控制器的 model.addattribute() 进入 .jsp。如果我将 itemValue 更改为“sID”,这是我的“结构”表中的一个字段,它应该将所选对象的 id 传递给 getStruct() 方法。但是,它也不起作用,它只是给了我“0”,这意味着我实际上没有得到一个 ID。
    • 也许我和你不太一样,因为你的所有代码都不在这里。看看这个例子。他甚至有一个可下载的例子来玩。 mkyong.com/spring-mvc/spring-mvc-dropdown-box-example
    猜你喜欢
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    • 1970-01-01
    相关资源
    最近更新 更多