Java Spring MVC 在同一个 JSP 中获取/发布
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5834938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Spring MVC get/post in same JSP
提问by Jaanus
this is from my controller..
这是来自我的控制器..
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String add(Model model) {
return "add";
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String added(@RequestParam("name") String name, Model model) {
City city = new City();
city.setCity(name);
service.addCity(city);
return "add";
}
this is my JSP view..this is only for adding at the moment...this is add.jsp..so it posts back to itself
这是我的 JSP 视图..这仅用于目前添加......这是 add.jsp..所以它回传给自己
<form method="post" action="/spring/krams/edit/add">
Linna nimi
<input type="text" name="name">
<input type="submit" value="Test" name="submit" />
</form>
i would like the JSP file change , so that when i post it to this file, then it says..."CITY ADDED" . is that possible?
我想更改 JSP 文件,以便当我将其发布到此文件时,它会显示...“添加了城市”。那可能吗?
WHAT ABOUT UPDATING A CITY??
更新一个城市怎么样??
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String updated(@RequestParam("city") int city_id,
@RequestParam("text") String name,
Model model) {
service.updateCity(name, city_id);
return "update";
}
Here is no object?
这里没有对象?
回答by Javi
In the post method you can add an attribute with addAttribute method
在 post 方法中,您可以使用 addAttribute 方法添加属性
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String added(@RequestParam("name") String name, Model model) {
City city = new City();
city.setCity(name);
service.addCity(city);
model.addAttribute("city", city);
return "add";
}
and in the JSP you can check if the attribute city is null or not (with the tag <c:if/>
). In case it is not null, it is because it has just been added to the model, so you can show whatever you want. ${city.city}
its just a JSTL expression which accesses to the city attribute and then call the getter to access to the city property of that attribute:
并且在 JSP 中,您可以检查属性 city 是否为空(使用标记<c:if/>
)。如果它不为空,那是因为它刚刚添加到模型中,因此您可以显示任何您想要的内容。${city.city}
它只是一个 JSTL 表达式,它访问 city 属性,然后调用 getter 来访问该属性的 city 属性:
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<c:if test="${city != null}">
CITY <c:out value="${city.city}" /> ADDED
</c:if>
UPDATE
更新
If you need different messages depending on update/create operations you can do this: (In the example an update is done when the id param is not null because the id is the identifier of the city to update)
如果根据更新/创建操作需要不同的消息,您可以执行以下操作:(在示例中,当 id 参数不为 null 时完成更新,因为 id 是要更新的城市的标识符)
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String added(@RequestParam(value="id", required=false) String id, @RequestParam("name") String name, Model model) {
City city;
String operation;
if(id== null){
//create operation
city = new City();
operation = "CREATE";
}else{
//update operation
city = service.findCity(id);
operation = "UPDATE";
}
city.setCity(name);
service.saveCity(city); //save or update
model.addAttribute("city", city);
model.addAttribute("operation", operation); //add operation param
return "add";
}
and in the JSP you can do:
在 JSP 中,您可以执行以下操作:
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<c:if test="${operation == 'CREATE'}">
<c:if test="${city != null}">
CITY <c:out value="${city.city}" /> ADDED
</c:if>
<c:if test="${operation == 'UPDATE'}">
CITY <c:out value="${city.city}" /> UPDATED
</c:if>
</c:if>