- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当我编辑实体时,我想在下拉列表中设置类别的选定值。但我无法按照我尝试的方式做到这一点。我是 spring mvc 的新手,但我确信有更好的方法将模型映射到 View 。您能提供一些例子吗?
editMeal.jsp
<%@ include file="/WEB-INF/template/taglibs.jsp"%>
<div class="container">
<%@ include file="menu.jsp"%>
<form:form commandName="editForm" method="post"
class="form-horizontal form-width">
<fieldset>
<legend>Edit meal</legend>
<div class="form-group">
<input class="hidden" type="text" value="${idmeal}" hidden="true"
name="idmeal" />
<label for="name" class="col-lg-2 control-label">Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="name"
placeholder="Name" name="name" value="${name }">
</div>
</div>
<div class="form-group">
<label for="select" class="col-lg-2 control-label">Category</label>
<div class="col-lg-10">
<select name="idCategory" class="form-control">
<option value="NONE">Select category</option>
<c:forEach items="${categories}" var="curCategory">
<c:choose>
<c:when test="${curCategory eq category}">
<option value= "${category.text}" selected="selected">${category.value}</option>
</c:when>
<c:otherwise>
<option value="${category.text}">${category.value}</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
</div>
</div>
<div class="form-group">
<label for="shortName" class="col-lg-2 control-label">Short name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="shortName"
placeholder="Short name" name="shortName" value="${shortName }">
</div>
</div>
<div class="form-group">
<label for="description" class="col-lg-2 control-label">Description</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="description"
placeholder="Description" name="description" value="${description }">
</div>
</div>
<div class="form-group">
<label for="price" class="col-lg-2 control-label">Price</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="price"
placeholder="Price" name="price" value="${price }">
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<a class="btn btn-warning"
href="http://localhost:8080/Catering/index/meals">Cancel</a>
<button type="submit" class="btn btn-warning">Submit</button>
</div>
</div>
</fieldset>
</form:form>
</div>
DropDownListItem.java
public class DropDownListItem {
private String text;
private String value;
public DropDownListItem(String t, String n) {
text = t;
value = n;
}
//getters and setters
}
MealController.java
package catering.web.controller;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import catering.web.data_access.CategoryDataAccess;
import catering.web.data_access.MealDataAccess;
import catering.web.data_access.MealSummaryDataAccess;
import catering.web.helper.DropDownListItem;
import catering.web.mapper.MealMapper;
import catering.web.model.Category;
import catering.web.model.Meal;
import catering.web.model.MealSummary;
import catering.web.view_model.MealViewModel;
@Controller
public class MealController {
protected static Logger logger = Logger.getLogger("controller");
@RequestMapping(value = "meals", method = RequestMethod.GET)
public String mealsPage(Model model, Authentication authentication){
logger.debug("Received request to show meals page(GET)");
List<MealSummary> meals = MealSummaryDataAccess.getMeals();
model.addAttribute("username", "You are logged in as " + authentication.getPrincipal());
model.addAttribute("list", meals);
return "meals";
}
@RequestMapping(value = "addMeal", method = RequestMethod.GET)
public String addMealGet(Model model, Authentication authentication){
populateModel(model);
model.addAttribute("username", "You are logged in as " + authentication.getPrincipal());
model.addAttribute("addForm", new MealViewModel());
return "addmeal";
}
@RequestMapping(value = "addMeal", method = RequestMethod.POST)
public String addMealPost(@ModelAttribute("addForm") MealViewModel mealVM, Model model, Authentication authentication){
populateModel(model);
model.addAttribute("username", "You are logged in as " + authentication.getPrincipal());
Meal meal = MealMapper.mapMealVMToMeal(mealVM);
MealDataAccess.insertMeal(meal);
return "redirect:meals";
}
@RequestMapping(value = "editMeal", method = RequestMethod.GET)
public String editMealGet(@RequestParam int id, Model model, Authentication authentication) {
logger.debug("Received request to show edit user page(GET)");
model.addAttribute("username", "You are logged in as " + authentication.getPrincipal());
populateModel(model);
Meal meal = MealDataAccess.getMealById(id);
MealViewModel mealMV = MealMapper.mapMealToMealVM(meal);
model.addAttribute("idmeal", mealMV.getIdMeal() );
model.addAttribute("name",mealMV.getName());
model.addAttribute("category",mealMV.getIdCategory());
model.addAttribute("shortName", mealMV.getShortName() );
model.addAttribute("description", mealMV.getDescription() );
model.addAttribute("price", mealMV.getPrice() );
return "editmeal";
}
@RequestMapping(value = "deleteMeal", method = RequestMethod.POST)
public String deleteMeal(@ModelAttribute("delete") @RequestParam int id){
logger.debug("Received request to delete meal page(POST)");
MealDataAccess.deleteMeal(id);
return "redirect:meals";
}
private void populateModel(Model model){
List<Category> cats = CategoryDataAccess.getCategories();
List<DropDownListItem> categories = new ArrayList<DropDownListItem>();
for(Category cat : cats){
categories.add(new DropDownListItem(Integer.toString(cat.getIdCategory()),cat.getName()));
}
model.addAttribute("categories", categories);
}
}
MealViewModel.java
public class MealViewModel {
private int idMeal;
private String name;
private String shortName;
private String description;
private String idCategory;
private float price;
//getters and setters
}
最佳答案
为此,您必须使用以下标签库:
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
按如下方式编写选择标签:
<form:form commandName="editMeal" method="post"
class="form-horizontal form-width">
<-- other code -->
<form:select id="categoryList" path="idCategory">
<c:forEach items="${categories}" var="cat">
<form:option value="${category.text}" label="${category.value}" />
</c:forEach>
</form:select>
</form:form>
路径将是模型属性editMeal中的属性,用于设置列表中的值。在 Controller 中设置 editMeal 属性,如下所示:
model.addAttribute("editMeal", mealMV);
关于java - JSP - 编辑实体时如何设置指定下拉列表中的选定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24735340/
我有十二个指向不同类别的链接。作为用户定位的一种方式,我想强调用户现在所在的类别( - 按钮)。 如何在 CSS 中实现这一点?我读到了 selected和 active ,但我还没能让它发挥作用。
我想通过单击按钮来获取选择框中的所有选项值(选定/未选定)。我怎样才能做到这一点? 最佳答案 我认为这是使用 Traversing/map 的好机会方法: var valuesArray = $("#
我正在尝试构建一个计算器,其中包含两个数字的两个 TextView 字段。我弄清楚了如何使用“应用程序内”数字键盘输入顶部数字 Operand 1 [textView] 的数字(我知道使用 EditT
我有一个简单的 jQuery $("span.value"),它选择包含文本的节点。此文本保证为整数。如何计算所有选定节点文本的总和,并将总和放入另一个节点? 3 4 5 ? 最佳答案 你可以这样做:
我从同一台服务器上托管的一堆数据库中备份了 mysql 数据库 db1。现在只需要备份具有访问 db1 权限的选定用户,以便我可以在 db1 还原之前将这些特权用户还原到我的新服务器。 最佳答案 St
我有一个 ListView 。我想添加一个动画,如果我选择一个列表项,它将被删除,并且该项目下方的其余项目将通过向上滑动动画向上移动。我已经通过获取其子位置使用线性布局完成了此操作,但我无法理解如何向
我不明白如何使用 Python 解析来自 Outlook 的突出显示(选定)邮件? 我有这个代码,但它适用于上一封邮件。 import win32com.client outlook = win32c
在 Xcode 6 中,您现在可以为选项卡项目的选中和未选中状态设置图标。请参阅下图中的说明: 和 唯一的问题是 SELECTED 状态的图像不显示。它只是显示空白。还有其他人有这个问题吗?请看下面的
在我的数据模型中,我有一个实体组和另一个GroupMember实体。一个Group包含一个或多个GroupMembers,但一个GroupMember只能同时位于一个Group中。到目前为止没有问题,
Android Button 在不同状态(正常、按下、选中、禁用)之间移动时会更改其可绘制背景。背景切换是即时的。是否可以使其平滑(动画)? 最佳答案 是的,这是可能的。您只需为按钮添加 addAni
我使用 emacs 来查看和编辑代码和其他文本文件。我想知道是否有一种方法可以向前或向后搜索当前缓冲区中标记的文本。类似于我在记事本或写字板中可以执行的操作。就像我可以在缓冲区中标记一些文本并执行 C
如何根据状态(选定、禁用)设置自定义选择类?如何根据状态选择(选定、禁用)在自定义下拉列表中设置类?照做了,但什么也没发生。请看我的例子................................
我正在尝试检查下拉菜单中是否存在特定文本值,如果存在,我想将其属性设置为selected。 我成功编写了一个 if 语句来检查文本是否存在: var country = usr.location; i
对于我的应用程序,我想让用户能够在回收器 View 中调整 TextView 项目的文本大小(通过捏缩放或 SeekBar)。默认值应为系统设置中选择的文本大小。最小值应为系统设置中的“非常小”,最大
我正在尝试创建一个 ListBoxItem 模板,该模板在选择时将带有圆角边框。我得到了这个 xaml,它在选择时不起作用:
我正在寻找检索焦点元素的 HTML。查看 webdriver.io 文档,方法 .getActiveElement() 应该可以解决这个问题。但是,我的 IDE (WebStorm) 显示错误,指出它
我创建了一个圆,在我的 onDraw() 方法中围绕圆绘制了一条字符串和一条线(箭头)。 public class Circle extends Activity { public class
对于生产应用程序,我们希望在 Windows 窗体应用程序的 ElementHost 内显示 DatePicker,但我们遇到了 SelectedDate 和 CurrentDate 不可读的问题,因
好的,我在此处和 Google 上的许多网站上搜索了此问题的结果,但找不到针对我的问题的确切解决方案。 我有一个 sql 提取姓名和办公室。所以事情是这样的: $sql = "SELECT m
选中单元格时如何改变灰色? 最佳答案 当用户点击选中的行 (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSInd
我是一名优秀的程序员,十分优秀!