在mvc spring中将数据从视图传递到控制器

时间:2017-04-10 10:52:57

标签: java spring spring-mvc

我想将项目对象从视图传递到控制器并查找问题。寻找你的帮助来解决问题。请参考以下课程。 的 HelloController.java

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";  
    }
}

` 我怎么能这样做。你的帮助将不胜感激。

1 个答案:

答案 0 :(得分: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";  
}