我想对以下场景提出一些建议:我有一个分为选项卡的表单,每个选项卡代表自己的模型属性我想在用户请求页面时显示所有选项卡及其字段。
但是我希望他们每个人都有自己的控制器,所以当一个 POST 被单独处理时。当对页面发出请求时,我使用一个控制器来获取页面并准备列表框并填充它们并将其显示给用户。并非所有字段都显示为“惩罚”选项卡我只能在“惩罚”选项卡上看到“惩罚编号”标签。下面是我的代码:
Citizens_Registration.html
<%@ include file="/WEB-INF/jsp/include.jsp" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix = "fmt" uri= "http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix = "c" uri= "http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
<style>
<%@ include file="../css/forms.css" %>
</style>
<script type="text/javascript">
$(document).ready(function(){
$('#skinColorId').change(function(){
var skinId = $('select#skinColorId').val();
$('#skinColorPreviewer').css({backgroundColor: skinId});
});
$('#eyeColorId').change(function(){
var eyeId = $('select#eyeColorId').val();
$('#eyeColorPreviewer').css({backgroundColor: eyeId});
});
$('#hairColorId').change(function(){
var hairId = $('select#hairColorId').val();
$('#hairColorPreviewer').css({backgroundColor: hairId});
});
//$("#divLeft").height($("#divRight").height());
});
$(function() {
$( "#tabs" ).tabs();
});
</script>
<title>Citizen Registration</title>
</head>
<body>
<div id="tabs">
<ul>
<li><a href="#tab1">Citizen Registration</a></li>
<li><a href="#tab2">Punishment</a></li>
</ul>
<!-- ---------------------------------------------------------------------CHARACTERISTICS INFO------------------------------------------------------------------------------------------------------------------------------------- -->
<div id="tab1">
<form:form id="citizenRegistration" name ="citizenRegistration" method="post" modelAttribute="citizens" action="citizen_registration.htm">
<fieldset>
<legend>Characteristics Info</legend>
<ol>
<li><form:label for="photo" path="photo">Select Photo</form:label>
<form:input path="photo" type="file" id="photo" title="Upload a photo"/><form:errors path="photo" id="errors"/></li>
<li>
<label>Select Gender</label>
<form:select path="genderId" id="genderId" title="Select Your Gender">
<form:options items = "${gender.genderList}" itemValue="genderId" itemLabel="genderDesc" />
</form:select>
<form:errors path="genderId" class="errors"/>
</li>
<li><form:label for="weight" path="weight">Enter Weight <i>(lbs)</i></form:label>
<form:input path="weight" id="weight" title="Enter Weight"/><form:errors path="weight" id="errors"/>
</li>
<li><form:label for="height" path="height">Enter Height <i>(feet)</i></form:label>
<form:input path="height" id="height" title="Enter Height"/><form:errors path="height" id="errors"/>
</li>
<li>
<form:label for="skinColorId" path="skinColorId">Select Skin Color</form:label>
<form:select path="skinColorId" id="skinColorId" title="Select Skin Color">
<form:options items = "${skinColor.skinColorList}" itemValue="colorCode" itemLabel="colorDesc" style="background-color: ${color}" />
</form:select>
<form:errors path="skinColorId" id="errors"/><label class="colorPreviewer" id="skinColorPreviewer">color previewer</label>
</li>
<li>
<form:label for="eyeColorId" path="eyeColorId">Select Eye Color</form:label>
<form:select path="eyeColorId" id="eyeColorId">
<form:options items = "${eyeColor.eyeColorList}" itemValue="colorCode" itemLabel="colorDesc"/>
</form:select>
<form:errors path="eyeColorId" id="errors"/><label class="colorPreviewer" id="eyeColorPreviewer">color previewer</label>
</li>
<li>
<form:label for="hairColorId" path="hairColorId">Select Hair Color</form:label>
<form:select path="hairColorId" id="hairColorId" title="Select Hair Color">
<form:options items = "${hairColor.hairColorList}" itemValue="colorCode" itemLabel="colorDesc" />
</form:select>
<form:errors path="hairColorId" id="errors"/><label class="colorPreviewer" id="hairColorPreviewer">color previewer</label>
</li>
<li><form:label for="noticeableFeatures" path="noticeableFeatures">Noticeable Features</form:label>
<form:textarea path="noticeableFeatures" id="noticeableFeatures" title="State Noticable Features e.g. scars, marks, moles....."/><form:errors path="noticeableFeatures" id="errors"/>
</li>
</ol>
</fieldset>
<fieldset>
<legend>Professional & Other Information</legend>
<ol>
<li>
<form:label for ="ethnicityId" path="ethnicityId">Select Ethnicity</form:label>
<form:select path="ethnicityId" id="ethnicityId" title="Select Ethnicity">
<form:options items = "${ethnicity.ethnicityList}" itemValue="ethnicityId" itemLabel="ethnicityDesc" />
</form:select>
<form:errors path="ethnicityId" id="errors"/>
</li>
<li>
<form:label for="occupationId" path="occupationId">Select Occupation</form:label>
<form:select path="occupationId" id="occupationId" title="Select Occupation">
<form:options items = "${occupation.occupationList}" itemValue="occupationId" itemLabel="occupationDesc" />
</form:select>
<form:errors path="occupationId" id="errors"/>
</li>
<li>
<form:label for="maritalStatusId" path="maritalStatusId">Select Marital Status</form:label>
<form:select path="maritalStatusId" id="maritalStatusId" title="Select Marital Status">
<form:options items = "${maritalStatus.maritalStatusList}" itemValue="maritalStatusId" itemLabel="maritalStatusDesc" />
</form:select>
<form:errors path="maritalStatusId" id="errors"/>
</li>
</ol>
</fieldset>
<fieldset>
<legend>Identity Information</legend>
<ol>
<li>
<label>Social Security No</label>
<form:input path="socialSecurityNumber"/><form:errors path="socialSecurityNumber" id="errors"/><label id="socialSecurityNumber"></label>
</li>
<li>
<form:label for="citizenTypeId" path="CitizenTypeId">Select Citizen Type</form:label>
<form:select path="citizenTypeId" id="citizenTypeId" title="Select Citizen Type">
<form:options items = "${citizenType.citizenTypeList}" itemValue="citizenTypeId" itemLabel="citizenTypeDesc" />
</form:select>
<form:errors path="citizenTypeId" id="errors"/>
</li>
<li><label>First Name</label>
<form:input path="fName" id="fName" title="Enter First Name"/><form:errors path="fName" id="errors"/><label id="fName"></label></li>
<li><label>Last Name</label>
<form:input path="lName" id="lName" title="Enter Last Name"/><form:errors path="lName" id="errors"/></li>
<li><label>Other Name / Nick Name</label>
<form:input path="oName" id="oName" title="Enter Other Name"/><form:errors path="oName" id="errors"/></li>
<li><label>Date Of Birth</label>
<form:input path="dob" id="dob" title="Date Of Birth"/><form:errors path="dob" id="errors"/></li>
</ol>
</fieldset>
<fieldset>
<legend>Contact Information</legend>
<ol>
<li>
<form:label for="countryId" path="countryId">Select Country</form:label>
<form:select path="countryId" id="countryId" title="Select Country">
<form:options items = "${country.countryList}" itemValue="countryId" itemLabel="countryName" />
</form:select>
<form:errors path="countryId" id="errors"/>
</li>
<li><form:label for="addLn1" path="addLn1">Address Line 1</form:label>
<form:input path="addLn1" id="addLn1" title="Enter Address Line 1"/><form:errors path="addLn1" id="errors"/>
</li>
<li><form:label for ="addLn2" path="addLn2">Address Line 2</form:label>
<form:input path="addLn2" id="addLn2" title="Enter Address Line 2"/><form:errors path="addLn2" id="errors"/>
</li>
<li><form:label for ="addLn3" path="addLn3">Address Line 3</form:label>
<form:input path="addLn3" id="addLn3" title="Enter Address Line 3"/><form:errors path="addLn3" id="errors"/>
</li>
<li>
<form:label for="phoneNo" path="phoneNo">Phone Number</form:label>
<form:input path="phoneNo" id="phoneNo" title="Enter Phone Number"/><form:errors path="phoneNo" id="errors"/>
</li>
</br>
<li><input type="submit" name= "user_request" value="Save" />
<input type="submit" name= "user_request" value="Update" />
</li>
</ol>
<label id="results">${results}</label>
<label id="Error">${error}</label>
</fieldset>
</form:form>
</div>
<!-- ---------------------------------------------------------------------------TAB 2--------------------------------------------------------------------------------------------------------------------------------------------- -->
<div id="tab2">
<form:form id="citizenRegistration" name ="citizenRegistration" method="post" modelAttribute="punishment" action="punishment_registration.htm">
<ol>
<li>
<form:label for ="punishmentId" path="punishmentId">Punishment Number</form:label>
<form:input path="punishmentId" id="punishmentId"/><form:errors path="punishmentId" id="errors"/>
</li>
<li>
<form:label for="crimeRecNo" path="crimeRecNo">Select Crime</form:label>
<form:select path="crimeRecNo" id="CrimeRecNo" title="Select Crime">
<form:options items = "${crime.crimeList}" itemValue="crimeRecNo" itemLabel="crimeRecNo"/>
</form:select>
<form:errors path="crimeRecNo" id="errors"/>
</li>
<li>
<form:label for="punishmentTypeId" path="punishmentTypeId">Select Crime</form:label>
<form:select path="punishmentTypeId" id="punishmentTypeId" title="Select Punishment Type">
<form:options items = "${punishmentType.punishmentTypeList}" itemValue="punishmentTypeId" itemLabel="punishmentTypeDesc"/>
</form:select>
<form:errors path="punishmentTypeId" id="errors"/>
</li>
<li>
<form:label for ="monitoringStDate" path="monitoringStDate"> Start Date </form:label>
<form:input path="monitoringStDate" id="monitoringStDate"/><form:errors path="monitoringStDate" id="errors"/>
</li>
<li>
<form:label for ="monitoringEnDate" path="monitoringEnDate"> End Date </form:label>
<form:input path="monitoringEnDate" id="monitoringEnDate"/><form:errors path="monitoringEnDate" id="errors"/>
</li>
</ol>
</form:form>
</div>
</div>
</body>
</html>
CitizenRegistrationController.java
进口…………
@Controller
public class CitizenRegistrationController {
private final Logger logger = Logger.getLogger(getClass());
@Autowired
private CitizenTypeManager citizenTypeManager;
@Autowired
private ColorManager colorManager;
@Autowired
private CountryManager countryManager;
@Autowired
private EthnicityManager ethnicityManager;
@Autowired
private GenderManager genderManager;
@Autowired
private MaritalStatusManager maritalStatusManager;
@Autowired
private OccupationManager occupationManager;
@Autowired
private CrimeManager crimeManager;
@Autowired
private PunishmentTypeManager punishmentTypeManager;
Map<String, Object> myCitizenType = new HashMap<String, Object>();
Map<String, Object> myGender = new HashMap<String, Object>();
Map<String, Object> myMaritalStatus = new HashMap<String, Object>();
Map<String, Object> myOccupation = new HashMap<String, Object>();
Map<String, Object> myHairColor = new HashMap<String, Object>();
Map<String, Object> mySkinColor = new HashMap<String, Object>();
Map<String, Object> myEyeColor = new HashMap<String, Object>();
Map<String, Object> myCountry = new HashMap<String, Object>();
Map<String, Object> myEthnicity = new HashMap<String, Object>();
Map<String, Object> myCrimes = new HashMap<String, Object>();
Map<String, Object> myPunishmentType = new HashMap<String, Object>();
@InitBinder("citizens")
protected void initBinder(WebDataBinder binder){
//removes white spaces
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
//formats date
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//By passing true this will convert empty strings to null
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
dateFormat.setLenient(false);
//binder.setValidator(new OfficerRegistrationValidation());
//binder.setValidator(officerRegistrationValidation);
}
//---------------------------------------------------------------------------------------------------------------
@RequestMapping(value="citizen_registration.htm", method = RequestMethod.GET)
public ModelAndView loadPage(HttpServletRequest request,
HttpServletResponse response,
@ModelAttribute Citizens citizen, Punishment punishment,
BindingResult result,
ModelMap m, Model model) throws Exception {
try{
logger.debug("In Http method for CitizenRegistrationController");
myCitizenType.put("citizenTypeList", this.citizenTypeManager.getCitizenType());
myGender.put("genderList", this.genderManager.getGenderList());
myMaritalStatus.put("maritalStatusList", this.maritalStatusManager.getMaritalStatus());
myOccupation.put("occupationList", this.occupationManager.getOccupations());
myHairColor.put("hairColorList", this.colorManager.getHairColors());
mySkinColor.put("skinColorList", this.colorManager.getSkinColors());
myEyeColor.put("eyeColorList", this.colorManager.getEyeColors());
myCountry.put("countryList", this.countryManager.getCountries());
myEthnicity.put("ethnicityList", this.ethnicityManager.getEthnicity());
myCrimes.put("crimeList", this.crimeManager.getCrimes());
myPunishmentType.put("punishmentTypeList", this.punishmentTypeManager.getPunishmentType());
model.addAttribute("gender", myGender);
model.addAttribute("citizenType", myCitizenType);
model.addAttribute("maritalStatus", myMaritalStatus);
model.addAttribute("occupation", myOccupation);
model.addAttribute("hairColor", myHairColor);
model.addAttribute("skinColor", mySkinColor);
model.addAttribute("eyeColor", myEyeColor);
model.addAttribute("country", myCountry);
model.addAttribute("ethnicity", myEthnicity);
model.addAttribute("crime", myCrimes);
model.addAttribute("punishmentType",myPunishmentType);
return new ModelAndView("citizen_registration");
}catch(Exception e){
logger.error("Exception in CitizenRegistrationController - ModelAndView loadPage "+e);
request.setAttribute("error",e.getMessage());
return new ModelAndView("error_page");
}
}
//--------------------------------------------------------------------------------------------------------------
@RequestMapping(value="punishment_registration.htm", method = RequestMethod.POST)
public ModelAndView loadPage(HttpServletRequest request,
HttpServletResponse response,
@ModelAttribute Punishment punishment,
BindingResult result,
ModelMap m, Model model) throws Exception {
try{
logger.debug("In Http method for CitizenRegistrationController - Punishment Registration");
//TODO
return new ModelAndView("punishment_registration");
}catch(Exception e){
logger.error("Exception in CitizenRegistrationController - - Punishment Registration : ModelAndView loadPage "+e);
request.setAttribute("error",e.getMessage());
return new ModelAndView("error_page");
}
}
/**
* @param citizenTypeManager the citizenTypeManager to set
*/
public void setCitizenTypeManager(CitizenTypeManager citizenTypeManager) {
this.citizenTypeManager = citizenTypeManager;
}
/**
* @param colorManager the colorManager to set
*/
public void setColorManager(ColorManager colorManager) {
this.colorManager = colorManager;
}
/**
* @param countryManager the countryManager to set
*/
public void setCountryManager(CountryManager countryManager) {
this.countryManager = countryManager;
}
/**
* @param ethnicityManager the ethnicityManager to set
*/
public void setEthnicityManager(EthnicityManager ethnicityManager) {
this.ethnicityManager = ethnicityManager;
}
/**
* @param genderManager the genderManager to set
*/
public void setGenderManager(GenderManager genderManager) {
this.genderManager = genderManager;
}
/**
* @param maritalStatusManager the maritalStatusManager to set
*/
public void setMaritalStatusManager(MaritalStatusManager maritalStatusManager) {
this.maritalStatusManager = maritalStatusManager;
}
/**
* @param occupationManager the occupationManager to set
*/
public void setOccupationManager(OccupationManager occupationManager) {
this.occupationManager = occupationManager;
}
/**
* @param myCrimes the myCrimes to set
*/
public void setMyCrimes(Map<String, Object> myCrimes) {
this.myCrimes = myCrimes;
}
/**
* @param punishmentTypeManager the punishmentTypeManager to set
*/
public void setPunishmentTypeManager(PunishmentTypeManager punishmentTypeManager) {
this.punishmentTypeManager = punishmentTypeManager;
}
}