Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 来自单一jsp表单spring mvc的多模型属性_Java_Spring_Jsp_Spring Mvc - Fatal编程技术网

Java 来自单一jsp表单spring mvc的多模型属性

Java 来自单一jsp表单spring mvc的多模型属性,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,我创建了一个jsp表单,希望将数据发送到两个表。这意味着我创建了两个模型类,所以表单应该引用这两个模型类。我试过了,但失败了。如何从一个jsp页面获取两个modelAttribute,并分配给一个控制器。提前谢谢 `@Entity @Table(name = "survey") public class Survey { // all anotations and getters and setters are omitted private int surveyId;

我创建了一个jsp表单,希望将数据发送到两个表。这意味着我创建了两个模型类,所以表单应该引用这两个模型类。我试过了,但失败了。如何从一个jsp页面获取两个modelAttribute,并分配给一个控制器。提前谢谢

`@Entity
@Table(name = "survey")
public class Survey {
    // all anotations and getters and setters are omitted
    private int surveyId;
    private String surveyName;

}

@Entity
@Table(name = "preferred")
public class Survey {
    // all anotations and getters and setters are omitted
    private int preferredId;
    private String preferredClass;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="surveyId")
    private Survey survey;
}
jsp表单

<spring:url value="/survey/save" var="saveURL"></spring:url>
<form:form action="${saveURL}" method="POST" modelAttribute="surveyForm">

    //Survey model class
    <form:hidden path="surveyId" /> 
    <form:input path="surveyName" />

    //preferredClass model class
    <form:input path="preferredClass">      

    <button type="submit" >Submit</button>  

</form:form>

我是这样认识到你的问题的: 在控制器中需要一个api来返回两种类型的数据

IActionResult.java

public interface IActionResult<T> {
}
public class ActionResult<T> extends ActionResultBase<T> {
}
公共接口IActionResult{
}
ActionResultBase.java

public abstract class ActionResultBase<T> implements 

    IActionResult<T>,Serializable {
    }
公共抽象类ActionResultBase实现
IActionResult,可序列化{
}
ActionResult.java

public interface IActionResult<T> {
}
public class ActionResult<T> extends ActionResultBase<T> {
}
公共类ActionResult扩展了ActionResultBase{
}
并将您的实体更改为

public class Survey1 implements Serializable,IActionResult<Survey1> 

 public class Survey2 implements Serializable,IActionResult<Survey2> 
public class Survey1实现可序列化的IActionResult
公共类Survey2实现可序列化的IActionResult
在你的控制器里

@RequestMapping(value = "/save", method = RequestMethod.POST)
 //here I need to get ModelAttribuete of Survey and preferredClass
 public ResponseEntity<IActionResult> saveSurvey( @ModelAttribute("surveyForm") Survey survey)
    {
       if(your desired condition 1){
           //do your Business
           return new ResponseEntity<IActionResult>(survay1, HttpStatus.OK(any things you like);
           }

        if(your desired condition 2){                
           //do your Business    
           return new ResponseEntity<IActionResult>(survay2, tpStatus.OK(any things you like);
            }

}
@RequestMapping(value=“/save”,method=RequestMethod.POST)
//在这里,我需要获得调查的ModelAttribute和preferredClass
公共响应属性保存调查(@ModelAttribute(“surveyForm”)调查)
{
如果(您想要的条件1){
//做你的事
返回新的ResponseEntity(survay1,HttpStatus.OK)(任何您喜欢的内容);
}
如果(你想要的条件2){
//做你的事
返回新的ResponseEntity(survay2,tpStatus.OK)(任何你喜欢的东西);
}
}

我希望这就是您要找的。

要么在前端创建两个表单,要么创建一个复合对象并在表单上使用。我是spring的新手,您能给我一个例子吗?为什么两个相同的测量对象?不,我需要preferredClass对象