gpt4 book ai didi

java - JSP - 编辑实体时如何设置指定下拉列表中的选定值?

转载 作者:太空宇宙 更新时间:2023-11-04 06:40:32 25 4
gpt4 key购买 nike

当我编辑实体时,我想在下拉列表中设置类别的选定值。但我无法按照我尝试的方式做到这一点。我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com