我希望在满足其他变量的条件后显示变量列表。例如:当表中共有5个变量时,即Name,Id,Age,Experience,Salary。首先,它必须只显示Id,Name和Experience。一旦我在体验栏中输入值3或更多,那么它必须显示另外两列(薪水和年龄)并提交。或者,当我输入一个小于3的值时,它只存储表中的前3个变量。在程序中,我必须显示帐号详细信息,只要我输入的经验超过3或者它必须在数据库中单独显示剩余的列。这个操作是在Spring MVC中使用eclipse在CRUD操作中完成的任何线索? 我附上了我的程序代码
Bean类:
package com.dineshonjava.bean;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import com.dineshonjava.validator.Ajay;
@Entity
@Table(name="employees")
@SecondaryTable(name="employeedetails")
public class EmployeeBean {
private Integer id;
@NotEmpty
@Size(min=2,max=20)
private String name;
@NotNull
@Min(18)@Max(90)
private Integer age;
@NotEmpty @Email
private String email;
@NotNull
private Integer salary;
@NotEmpty
private String address;
@NotEmpty
private String bae;
@NotNull
private Integer experience;
@NotEmpty
@Size(min = 6, max = 15)
private String password;
@NotEmpty
@Ajay
private String phone;
@NotNull
@Column(table="employeedetails")
private Integer accno;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
this.salary = salary;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getBae() {
return bae;
}
public void setBae(String bae) {
this.bae = bae;
}
public Integer getExperience() {
return experience;
}
public void setExperience(Integer experience) {
this.experience = experience;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Integer getAccno() {
return accno;
}
public void setAccno(Integer accno) {
this.accno = accno;
}
}
控制器类:
package com.dineshonjava.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
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.servlet.ModelAndView;
import com.dineshonjava.bean.EmployeeBean;
import com.dineshonjava.model.Employee;
import com.dineshonjava.service.EmployeeService;
@Controller
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveEmployee(@Valid @ModelAttribute("command") EmployeeBean employeeBean,
BindingResult result) {
if(result.hasErrors()){
return new ModelAndView("addEmployee");
}
Employee employee = prepareModel(employeeBean);
employeeService.addEmployee(employee);
Map<String, Object> model = new HashMap<String, Object>();
model.put("employees", prepareListofBean(employeeService.listEmployeess()));
return new ModelAndView("displayEmployee",model);
}
@RequestMapping(value = "/saved", method = RequestMethod.POST)
public ModelAndView savedEmployee(@Valid @ModelAttribute("command") EmployeeBean employeeBean,
BindingResult result) {
if(result.hasErrors()){
return new ModelAndView("addEmployee");
}
Employee employee = prepareModel(employeeBean);
employeeService.addEmployee(employee);
Map<String, Object> model = new HashMap<String, Object>();
model.put("employees", prepareListofBean(employeeService.listEmployeess()));
return new ModelAndView("displayEmployee",model);
}
@RequestMapping(value="/employees", method = RequestMethod.GET)
public ModelAndView listEmployees() {
Map<String, Object> model = new HashMap<String, Object>();
model.put("employees", prepareListofBean(employeeService.listEmployeess()));
return new ModelAndView("employeesList", model);
}
@RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView addEmployee(@Valid @ModelAttribute("command") EmployeeBean employeeBean,
BindingResult result) {
if(result.hasErrors()){
return new ModelAndView("addEmployee");
}
Map<String, Object> model = new HashMap<String, Object>();
model.put("employees", prepareListofBean(employeeService.listEmployeess()));
return new ModelAndView("addEmployee", model);
}
@RequestMapping(value = "/index", method = RequestMethod.GET)
public ModelAndView welcome() {
return new ModelAndView("index");
}
@RequestMapping(value = "/delete", method = RequestMethod.GET)
public ModelAndView editEmployee(@ModelAttribute("command") EmployeeBean employeeBean,
BindingResult result) {
employeeService.deleteEmployee(prepareModel(employeeBean));
Map<String, Object> model = new HashMap<String, Object>();
model.put("employee", null);
model.put("employees", prepareListofBean(employeeService.listEmployeess()));
return new ModelAndView("displayEmployee", model);
}
@RequestMapping(value = "/edit", method = RequestMethod.GET)
public ModelAndView deleteEmployee(@Valid @ModelAttribute("command") EmployeeBean employeeBean,
BindingResult result) {
if(result.hasErrors()){
Map<String, Object> model = new HashMap<String, Object>();
model.put("employee", prepareEmployeeBean(employeeService.getEmployee(employeeBean.getId())));
model.put("employees", prepareListofBean(employeeService.listEmployeess()));
return new ModelAndView("editEmployee", model);
}
Map<String, Object> model = new HashMap<String, Object>();
model.put("employee", prepareEmployeeBean(employeeService.getEmployee(employeeBean.getId())));
model.put("employees", prepareListofBean(employeeService.listEmployeess()));
return new ModelAndView("displayEmployee", model);
}
private Employee prepareModel(EmployeeBean employeeBean){
Employee employee = new Employee();
employee.setAddress(employeeBean.getAddress());
employee.setAge(employeeBean.getAge());
employee.setName(employeeBean.getName());
employee.setSalary(employeeBean.getSalary());
employee.setId(employeeBean.getId());
employee.setBae(employeeBean.getBae());
employee.setExperience(employeeBean.getExperience());
employee.setEmail(employeeBean.getEmail());
employee.setPassword(employeeBean.getPassword());
employee.setPhone(employeeBean.getPhone());
employee.setAccno(employeeBean.getAccno());
employeeBean.setId(null);
return employee;
}
private List<EmployeeBean> prepareListofBean(List<Employee> employees){
List<EmployeeBean> beans = null;
if(employees != null && !employees.isEmpty()){
beans = new ArrayList<EmployeeBean>();
EmployeeBean bean = null;
for(Employee employee : employees){
bean = new EmployeeBean();
bean.setName(employee.getName());
bean.setId(employee.getId());
bean.setAddress(employee.getAddress());
bean.setSalary(employee.getSalary());
bean.setAge(employee.getAge());
bean.setBae(employee.getBae());
bean.setExperience(employee.getExperience());
bean.setEmail(employee.getEmail());
bean.setPassword(employee.getPassword());
bean.setPhone(employee.getPhone());
bean.setAccno(employee.getAccno());
beans.add(bean);
}
}
return beans;
}
private EmployeeBean prepareEmployeeBean(Employee employee){
EmployeeBean bean = new EmployeeBean();
bean.setAddress(employee.getAddress());
bean.setAge(employee.getAge());
bean.setName(employee.getName());
bean.setSalary(employee.getSalary());
bean.setId(employee.getId());
bean.setBae(employee.getBae());
bean.setExperience(employee.getExperience());
bean.setEmail(employee.getEmail());
bean.setPassword(employee.getPassword());
bean.setPhone(employee.getPhone());
bean.setAccno(employee.getAccno());
return bean;
}
}
EmployeeDisplay.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form"
prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring MVC Form Handling</title>
</head>
<style>
.error {
color: red; font-weight: bold;
}
</style>
<body>
<h2>Add Employee Data</h2>
<form:form method="POST" action="/sdnext/save.html">
<table>
<tr>
<td align="left" width="20%">EmployeeID: </td>
<td align="left" width="40%"><form:input path="id" readonly="true" size="30"/></td>
</tr>
<tr>
<td align="left" width="20%">Name: </td>
<td align="left" width="40%"><form:input path="name" size="30"/></td>
<td><form:errors path="name" cssClass="error"/></td>
</tr>
<tr>
<td align="left" width="20%">Age: </td>
<td align="left" width="40%"><form:input path="age" size="30"/></td>
<td align="left"><form:errors path="age" cssClass="error"/></td>
</tr>
<tr>
<td align="left" width="20%">salary: </td>
<td align="left" width="40%"><form:input path="salary" size="30"/></td>
<td align="left"><form:errors path="salary" cssClass="error"/></td>
</tr>
<tr>
<td align="left" width="20%">Address: </td>
<td align="left" width="40%"><form:input path="address" size="30"/></td>
<td><form:errors path="address" cssClass="error"/></td>
</tr>
<tr>
<td align="left" width="20%">Bae: </td>
<td align="left" width="40%"><form:input path="bae" size="30"/></td>
<td><form:errors path="bae" cssClass="error"/></td>
</tr>
<tr>
<td align="left" width="20%">Experience: </td>
<td align="left" width="40%"><form:input path="experience" size="30"/></td>
<td><form:errors path="experience" cssClass="error"/></td>
</tr>
<tr>
<td align="left" width="20%">Email: </td>
<td align="left" width="40%"><form:input path="email" size="30"/></td>
<td><form:errors path="email" cssClass="error"/></td>
</tr>
<tr>
<td>Password: </td>
<td><form:password path="password" size="30"/></td>
<td><form:errors path="password" cssClass="error"/></td>
</tr>
<tr>
<td align="left" width="20%">Phone: </td>
<td align="left" width="40%"><form:input path="phone" size="30"/></td>
<td><form:errors path="phone" cssClass="error"/></td>
</tr>
<tr>
<td align="left" width="20%">Accno: </td>
<td align="left" width="40%"><form:input path="accno" size="30"/></td>
<td><form:errors path="accno" cssClass="error"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit"/></td>
</tr>
</table></form:form>
</body>
</html>
EmployeeDisplay.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<c:if test="${!empty employees}">
<table align="left" border="1">
<tr>
<th>Employee ID</th>
<th>Employee Name</th>
<th>Employee Age</th>
<th>Employee Salary</th>
<th>Employee Address</th>
<th>Employee Bae</th>
<th>Employee Experience</th>
<th>Employee Email</th>
<th>Password</th>
<th>Phone</th>
<th>Account Number</th>
<th>Action Performed</th>
</tr>
<c:forEach items="${employees}" var="employee">
<tr>
<td><c:out value="${employee.id}"/></td>
<td><c:out value="${employee.name}"/></td>
<td><c:out value="${employee.age}"/></td>
<td><c:out value="${employee.salary}"/></td>
<td><c:out value="${employee.address}"/></td>
<td><c:out value="${employee.bae }"/></td>
<td><c:out value="${employee.experience }"/></td>
<td><c:out value="${employee.email}"/></td>
<td><c:out value="${employee.password}"/></td>
<td><c:out value="${employee.phone}"></c:out>
<td><c:out value="${employee.accno}"></c:out>
<td align="center"><a href="edit.html?id=${employee.id}">Edit</a> | <a href="delete.html?id=${employee.id}">Delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
答案 0 :(得分:0)
您可以在用户界面级别实现此功能。数据库将有5列。但是,当您只输入3列时,剩下的两列将为空。