使用spring在弹出窗口中提交数据后,在父窗口或其他窗口上填充数据

时间:2013-01-11 13:02:14

标签: spring-mvc

我的要求是我想在父jsp上填充数据,或者在使用spring关闭弹出时可能在其他jsp中。我面临的问题是我能够打开pop但是在提交值后数据被填充在同一个弹出窗口我希望在不同的页面上,例如我已经完成的弹出窗口我已经提交了'Employee_Id`数据的值弹出窗口本身我不知道如何使用弹簧填充不同页面上的数据这里是我的代码

这是我的控制器

package com.nousinfo.tutorial.controllers;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
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 org.springframework.web.servlet.ModelAndView;

import com.nousinfo.tutorial.model.EmployeeForm;
import com.nousinfo.tutorial.service.impl.EmployeeServiceImpl;
import com.nousinfo.tutorial.service.model.EmployeeBO;

@Controller
@RequestMapping("/search")
public class SearchEmployeeController {

    private EmployeeServiceImpl employeeServiceImpl;

    public void setEmployeeServiceImpl(EmployeeServiceImpl employeeServiceImpl) {
        this.employeeServiceImpl = employeeServiceImpl;
    }

    @RequestMapping(value = "/searchspring", method = RequestMethod.GET)
    public String view() throws Exception {
        return "Home";
    }

    @RequestMapping(value = "/searchPage", method = RequestMethod.GET)
    public ModelAndView search(@Validated EmployeeForm employeeForm,
            @RequestParam("selectedValue") String selectedValue)
            throws Exception {
        ModelAndView model = new ModelAndView();

        if (selectedValue.equals("FindEmployeeById")) {
            boolean x = true;
            model.addObject("x", x);
        }
        if (selectedValue.equals("FindEmployeeByName")) {

            boolean y = true;
            model.addObject("y", y);

        }

        if (selectedValue.equals("FindByDepartmentId")) {
            boolean z = true;
            model.addObject("z", z);
        }
        model.setViewName("Search");
        return model;
    }

    @RequestMapping(value = "/FindById", method = RequestMethod.GET)
    public ModelAndView searchByEmpNo(
            @ModelAttribute("employeeForm") EmployeeForm employeeForm)
            throws Exception {
        ModelAndView model = new ModelAndView();
        model.addObject("employeeForm", employeeForm);
        Integer i = (employeeForm.getEmployeeNumber());

        EmployeeBO employeeBO = employeeServiceImpl.getEmployee(i);
        System.out.println(employeeBO);
        model.addObject("employeeBO", employeeBO);
        model.addObject("block", "block");

        model.setViewName("EmployeeDetail");
        return model;
    }

    @RequestMapping(value = "/FindByName", method = RequestMethod.POST)
    public ModelAndView searchByEmployeeName(
            @ModelAttribute("employeeForm") EmployeeForm employeeForm) {
        ModelAndView model = new ModelAndView();
        model.addObject("employeeForm", employeeForm);
        List<EmployeeBO> employeeBOs = employeeServiceImpl
                .findEmployees(employeeForm.getFirstName());
        model.addObject("listEmployeeBO", employeeBOs);
        model.addObject("block", "block");
        model.setViewName("EmployeeList");
        return model;

    }

这是我的jsp页面

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>

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

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<fmt:setBundle basename="ApplicationResources" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Employee Search Page</title>
<script type="text/javascript">
    function doAllThese(url) {

        if (url == 'FindById') {
            document.form.action = "/EmployeeWebSpring/search/FindById";

        }
        if (url == 'FindByName') {
            document.form.action = "/EmployeeWebSpring/search/FindByName";
        }
        if (url == 'FindByDeptNO') {
            document.form.action = "/EmployeeWebSpring/search/FindByDeptNO";
        }
    }
    function loadName(name) {

        this.firstName = name;
        window.location = 'http://localhost:8080/EmployeeWeb/GetEmployee?key1='
                + encodeURIComponent(firstName);

    }
</script>
</head>
<body>
    <form:form name="form" commandName="employeeForm" method="post">



        <c:if test="${requestScope.x}">
            <div id="div1">
                Employee_ID:
                <form:input path="employeeNumber" />
                <input type="submit" name="method" value="FindById" id="FindById"
                    onclick="doAllThese(this.value)" />
            </div>
        </c:if>
        <c:if test="${requestScope.y}">

            <div id="div2" >

                Employee_Name
                <form:input path="firstName" />
                <input type="submit" name="method" value="FindByName"
                    onclick="doAllThese(this.value)" /> <br /> <font size=3>For
                    Searching the employees by<b>Employee Name</b><br />you can use %
                    match all the records with the given pattern
                </font><br /> <font size="2"> <i>e.g <b> for search by</b>EmployeeName<br />
                        matches alL the employees whose name starts with character <b>S</b></i></font>
            </div>
        </c:if>
        <c:if test="${requestScope.z}">
            <div id="div3">
                Employee_Name
                <form:input path="departmentId" />
                <input type="submit" name="method" value="FindByDeptNO"
                    onclick="doAllThese(this.value)" />
            </div>
        </c:if>
    </form:form>
</body>
</html>

这是我的ds-servlet xml,其中我使用内部视图解析器

  <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-3.2.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <context:annotation-config />

    <mvc:annotation-driven />

    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        id="dataSource">
        <property value="com.mysql.jdbc.Driver" name="driverClassName" />
        <property value="jdbc:mysql://192.168.25.30:3306/employee"
            name="url" />
        <property value="hr" name="username" />
        <property value="hr123" name="password" />
    </bean>
    <bean id="employeeDaoImpl" class="com.nousinfo.tutorial.employee.dao.impl.EmployeeDAOImpl">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="employeserviceImpl" class=" com.nousinfo.tutorial.service.impl.EmployeeServiceImpl">
        <property name="daoImpl" ref="employeeDaoImpl" />
    </bean>

    <bean id="mycontroller"
        class="com.nousinfo.tutorial.controllers.SearchEmployeeController">
        <property name="employeeServiceImpl" ref="employeserviceImpl"></property>
    </bean>
    <bean id="emplController" class="com.nousinfo.tutorial.controllers.EmployeeController">
        <property name="employeeServiceImpl" ref="employeserviceImpl"></property>
    </bean>



    <bean id="configurationLoader"
        class="org.springmodules.validation.bean.conf.loader.annotation.AnnotationBeanValidationConfigurationLoader" />
    <bean id="validator" class="org.springmodules.validation.bean.BeanValidator"
        p:configurationLoader-ref="configurationLoader" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/" p:suffix=".jsp" />

    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="com/nousinfo/resources/messages" />
    </bean>
    <!-- Configure the multipart resolver -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    </bean>

1 个答案:

答案 0 :(得分:0)

离开我的头顶,这是你的选择:

  1. 提交弹出窗口,将相关更新的数据存储在会话中或您可以访问的地方,然后使用JS调用父页面上的重新加载
  2. 加载父表AJAX,然后在弹出窗口解除后,调用AJAX代码重新加载页面内容。
  3. 想出一些非常复杂的JS来使用弹出窗口上的数据更新父页面......我认为这不是一个好的解决方案
  4. Spring MVC是一个服务器端工具,意味着所有数据都在服务器端进行处理。如果你想直接使用Spring获取任何新数据,你需要以这种或那种方式调用服务器。