我的实体中有sql.Date和sql.Time字段,想要使用文本输入编辑它们的值。
实体中的声明
@DateTimeFormat (pattern="HH:mm")
private Time time;
@DateTimeFormat (pattern="yyyy-MM-dd")
private Date date;
模板
<input id="date" name="date" type="text" th:value="*{{date}}" required="required" />
<input id="time" name="time" type="text" th:value="*{{time}}" />
这适用于显示值,但是当发布表单时,控制器内的时间和日期值都为null。有没有办法使这项工作?
源代码
import java.sql.Date;
import java.util.Calendar;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import murderstats.web.domain.FormHDetails;
@Controller
@RequestMapping(value = "/formh")
public class FormHController {
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test(@ModelAttribute("detailsItem") FormHDetails detailsItem) {
detailsItem.setDate(new Date(Calendar.getInstance().getTime().getTime()));
return "/formh/test";
}
@RequestMapping(value = "/test" , params={"save"})
public String test_save(@ModelAttribute("detailsItem") FormHDetails detailsItem,
final BindingResult bindingResult) {
// detailsItem.date is null here
return "redirect:/formh";
}
}
import java.sql.Date;
import org.springframework.format.annotation.DateTimeFormat;
public class FormHDetails {
@DateTimeFormat (pattern="yyyy-MM-dd")
private Date date;
}
<form action="#" th:action="@{/formh/test}" method="post" th:object="${detailsItem}">
<div>
<label for="date">Date</label>
<input id="date" name="date" type="text" th:field="*{date}" required="required" />
</div>
<button type="submit" name="save">Save</button>
</form>
答案 0 :(得分:1)
很少有东西...... 基本上,你的控制器没有正确地将对象传递给控制器,并且它没有收到他期望的......:
// GET Method
@RequestMapping(value={"/test"}, method = RequestMethod.GET)
public String getFormH(Model model){
FormHDetails detailsItem = new FormHDetails(); // or whatever constructor you use...
detailsItem.setDate(new DateTime().toDate); // import and use Joda Time is simpler...
model.addAttribute("itemAttribute", detailsItem);
return "/formh/test";
}
接下来,您需要在控制器中定义POST方法。您可以通过将请求映射中的方法更改为POST来执行此操作,并且您需要告诉控制器您希望表单以对象FormHDetails的形式提供信息:
@RequestMapping(value={"/test"}, method = RequestMethod.POST)
public postFormH(@ModelAttribute("itemAttribute") FormHDetails detailsItem, final BindingResult bindingResult) {
// check if the form is properly sent?
if(bindingResult.hasErrors(){
return "formh/test";
}
// If everything went fine, print the date and redirect me
System.out.println(detailsItem.date);
return "redirect:/formh";
}
好的,现在你应该有适当的工作控制器来处理GET和POST请求。
现在,在您的视图中,只需更改th:对象即可处理控制器发送的内容:
th:object="${itemAttribute}"
这应该有用......