gpt4 book ai didi

javascript - Spring MVC中当满足另一个变量的条件时如何从一个jsp重定向到另一个jsp

转载 作者:行者123 更新时间:2023-12-02 11:58:28 25 4
gpt4 key购买 nike

在 Spring MVC 上工作,当条件满足时,我想将页面从一个 JSP 重定向到另一个 JSP。

  1. 如果列经验大于3,则必须重定向到另一个JSP页面(当前JSP页面:addEmployee到insureEmployee JSP页面)
  2. 当该值小于3时,则必须正常将数据存储到数据库。
  3. 我有2个表...所以当经验超过3年时,它必须重定向到新的JSP,一旦我提交,数据必须存储在数据库(辅助表)中,否则当经验较少时大于 3 则必须保存到主表数据库
  4. 当我在体验列中输入超过 3 的值时,我只想重定向到另一个 JSP
  5. 我已经发布了我的 Controller 类和 JSP 代码

员工类的 Controller :

   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 = "/sub", method = RequestMethod.GET)
public ModelAndView Edit() {
return new ModelAndView("displayEmployee");
}

@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());

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());

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());

return bean;
}
}





Controller of Worker Class:
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.WorkerBean;
import com.dineshonjava.model.Worker;
import com.dineshonjava.service.WorkerService;
@Controller
public class WorkerController {
@Autowired
private WorkerService workService;
@RequestMapping(value = "/", method = RequestMethod.POST)
public ModelAndView saveWorker(@Valid @ModelAttribute("command") WorkerBean workBean,
BindingResult result) {
if(result.hasErrors()){

return new ModelAndView("");
}

Worker work = prepareModel(workBean);
workService.addWorker(work);
Map<String, Object> model = new HashMap<String, Object>();
model.put("works", prepareListofBean(workService.listWorkers()));
return new ModelAndView("",model);
}

@RequestMapping(value = "/added", method = RequestMethod.GET)
public ModelAndView added() {
return new ModelAndView("insureEmployee");
}
@RequestMapping(value = "/", method = RequestMethod.POST)
public ModelAndView savedWorker(@Valid @ModelAttribute("command") WorkerBean workBean,
BindingResult result) {
if(result.hasErrors()){

return new ModelAndView("");
}
Worker work = prepareModel(workBean);
workService.addWorker(work);
Map<String, Object> model = new HashMap<String, Object>();
model.put("works", prepareListofBean(workService.listWorkers()));
return new ModelAndView("",model);
}
@RequestMapping(value="/", method = RequestMethod.GET)
public ModelAndView listWorker() {
Map<String, Object> model = new HashMap<String, Object>();
model.put("works", prepareListofBean(workService.listWorkers()));
return new ModelAndView("", model);
}

@RequestMapping(value = "/insure", method = RequestMethod.GET)
public ModelAndView addWorker(@Valid @ModelAttribute("command") WorkerBean workBean,
BindingResult result) {
if(result.hasErrors()){

return new ModelAndView("insureEmployee");
}
Map<String, Object> model = new HashMap<String, Object>();
model.put("works", prepareListofBean(workService.listWorkers()));
return new ModelAndView("insureEmployee", model);
}

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView editEmployee(@ModelAttribute("command") WorkerBean workBean,
BindingResult result) {
workService.deleteWorker(prepareModel(workBean));
Map<String, Object> model = new HashMap<String, Object>();
model.put("work", null);
model.put("works", prepareListofBean(workService.listWorkers()));
return new ModelAndView("", model);
}

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView deleteWork(@Valid @ModelAttribute("command") Worker workBean,
BindingResult result) {
if(result.hasErrors()){
Map<String, Object> model = new HashMap<String, Object>();
model.put("work", prepareWorkerBean(workService.getWorker(workBean.getId())));
model.put("works", prepareListofBean(workService.listWorkers()));
return new ModelAndView("", model);
}
Map<String, Object> model = new HashMap<String, Object>();
model.put("work", prepareWorkerBean(workService.getWorker(workBean.getId())));
model.put("works", prepareListofBean(workService.listWorkers()));
return new ModelAndView("", model);
}

private Worker prepareModel(WorkerBean workBean){
Worker work = new Worker();
work.setPhone(workBean.getPhone());
work.setAccno(workBean.getAccno());
work.setId(workBean.getId());
workBean.setId(null);
return work;
}
private List<WorkerBean> prepareListofBean(List<Worker> work){
List<WorkerBean> beans = null;
if(work != null && !work.isEmpty()){
beans = new ArrayList<WorkerBean>();
WorkerBean bean = null;
for(Worker worker : work){
bean = new WorkerBean();

bean.setId(worker.getId());
bean.setPhone(worker.getPhone());
bean.setAccno(worker.getAccno());

beans.add(bean);
}
}
return beans;
}

private WorkerBean prepareWorkerBean(Worker work){
WorkerBean bean = new WorkerBean();

bean.setId(work.getId());
bean.setPhone(work.getPhone());
bean.setAccno(work.getAccno());

return bean;
}
}

必须重定向的addEmployee 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"%>

<!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" id="experience"
size="30" onblur="checkExperience()"/></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 colspan="2"><input type="submit" value="Submit" /></td>
</tr>
</table>


</form:form>
</body>
</html>

insureEmployee 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" %>

<!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>Insert title here</title>
</head>
<body>
<form:form method="POST" action="/sdnext/insure.html" >
<table> <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>Account Number:</td>
<td><form:password path="accno" size="30" /></td>
<td><form:errors path="accno" cssClass="error" /></td>
</tr>
<tr>
<td> <input type="submit" value="Submit"/></td>
</tr>
</table>

</form:form>
</body>
</html>


InsureEmployee where redirected page comes here:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>


<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

<!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>Insert title here</title>
</head>
<body>
<form:form method="POST" action="/sdnext/insure.html" >
<table> <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>Account Number:</td>
<td><form:password path="accno" size="30" /></td>
<td><form:errors path="accno" cssClass="error" /></td>
</tr>
<tr>
<td> <input type="submit" value="Submit"/></td>
</tr>
</table>

</form:form>
</body>
</html>

最佳答案

你能做的是

if(variable > 3){
return new ModelAndView("return to greater than 3 page");
}else{
return new ModelAndView("return to same page");
}

由于方法返回类型,您将需要重定向到同一页面或任何其他页面。

关于javascript - Spring MVC中当满足另一个变量的条件时如何从一个jsp重定向到另一个jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47434836/

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