2017-04-10 188 views
0

我想将视图中的项目对象传递给控制器​​并查找问题。寻找你的善意帮助解决问题。请参阅下面的课程。 HelloController.java在MVC中将数据从视图传递到控制器spring

import java.util.ArrayList; 
import java.util.List; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.client.RestTemplate; 

@Controller 
public class HelloController { 

@GetMapping("/hello") 
public String hello(Model model) { 
    final String uri = 
"http://localhost:8080/RESTfulExample/rest/json/metallica/get"; 

    RestTemplate restTemplate = new RestTemplate(); 
    List<FlashItem> result = restTemplate.getForObject(uri, ArrayList.class); 

    model.addAttribute("name", result); 

    return "welcome"; 
    } 
} 

` 的welcome.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> 
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<!DOCTYPE html> 
<html> 
<head> 
    <title>Spring 4 MVC Hello World Example with Maven Eclipse</title> 
    <link rel='stylesheet' href='<c:url value="/resources/css/style.css" />' 
    type='text/css' media='all' /> 
</head> 
<body> 
    <h2>Offer of the day</h2> 
    <form:form action="buyItem" method="post" modelAttribute="name"> 
    <c:forEach items="${name}" var="items" varStatus="us"> 
<p>Prdict Name: ${items.title}</p> 
<p>Description: ${items.description}</p> 
<p>Price: ${items.price}</p> 
<input type="submit" name="" value="Buy" > 
</c:forEach> 
</form:form> 
</body> 
</html> 

我想从拿的welcome.jsp项数据BuyController.java 购买Controller.java

import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.PostMapping; 
import org.springframework.web.bind.annotation.RequestParam; 

@Controller 
public class BuyController { 

    @PostMapping("/buyItem") 
    public String hello(@ModelAttribute("userForm") User user,Model model) { 
     System.out.println("Buy Itesms"); 
     return "user"; 
    } 
} 

` 我该如何做到这一点。您的帮助将不胜感激。

回答

0

不喜欢,作为一个例子,我如下:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> 
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <title>Spring 4 MVC Hello World Example with Maven Eclipse</title> 
     <link rel='stylesheet' href='<c:url value="/resources/css/style.css"/>' 
     type='text/css' media='all' /> 
    </head> 
    <body> 
     <h2>Offer of the day</h2> 

     <form:form action="foodItem" method="post" modelAttribute="user"> 
     <c:forEach items="${items}" var="item" varStatus="itemCount"> 
      <tr> 
       <td><input name="items[${itemCount}].title" 
        value="${item.name}"></td> 
      <td><input name="items[${itemCount}].description" 
        value="${item.name}"></td> 
      <td><input name="items[${itemCount}].price" 
        value="${item.price}"></td> 
      </tr> 
     </c:forEach> 
    </form:form > 
    </body> 
</html> 

然后在控制器类:

@PostMapping("/buyItem") 
public String hello(@ModelAttribute("user") User user,Model model) { 
    System.out.println("Buy Itesms"); 
    return "user"; 
} 
+0

感谢Subrata..on提交表单buyController的ITEMLIST不填充值。 –

+0

如果ItemList是模型/命令类/对象,并且它包含List 项目(再次包含标题和说明的Item类)。那么它应该工作。 –

+0

您最好提供详细代码,以便我可以看到您的实际问题在哪里 –

相关问题