0

在我的控制器中有不同的方法我想用一个表单动作来调用它们。我不知道如何将请求映射到具有不同提交按钮值的特定方法,当我运行我的索引页面时,它直接从它们转到控制器它可以从view()我的控制器呈现视图,并且在Search .jsp打开时我得到了我的输入字段上的默认0EmployeeId我不知道它为什么会发生请帮助我春天的新人

这是我的控制器

   package com.nousinfo.tutorial.controllers;

import java.util.List;

import org.springframework.stereotype.Controller;
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.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 ModelAndView view(
            @ModelAttribute("employeeForm") EmployeeForm employeeForm)
            throws Exception {
        ModelAndView model = new ModelAndView();
        model.addObject("employeeForm", employeeForm);
        model.setViewName("Search");

        return model;
    }

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

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

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

    @RequestMapping(value = "/empByName", 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.setViewName("EmployeeList");
        return model;

    }

    @RequestMapping(value = "/empByDeptId", method = RequestMethod.POST)
    public ModelAndView searchByDeptId(
            @ModelAttribute("employeeForm") EmployeeForm employeeForm) {
        ModelAndView model = new ModelAndView();
        model.addObject("employeeForm", employeeForm);
        List<EmployeeBO> employeeBOs = employeeServiceImpl
                .getAllEmployeeByDeptid(employeeForm.getDepartmentId());
        model.addObject("listEmployeeBO", employeeBOs);
        model.setViewName("EmployeeList");

        return model;

    }

}

这是我的 index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<%  
response.sendRedirect("/EmployeeWebSpring/search/searchspring");
%> 

这是我的 search.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>

</head>
<body>
    <form:form action="/EmployeeWebSpring/search/empByName"  commandName="employeeForm" method="post">
        <table border="0">



            <tr>
                <td>Employee_ID</td>
                <td><form:input path="employeeNumber"  /></td>
                <td><input type="submit" name="method" value="FindById" /></td>
            </tr>
            <tr>
                <td>Employee_Name</td>
                <td><form:input path="firstName" /></td>
                <td><input type="submit" name="method" value="FindByName" /></td>
            </tr>


            <tr>
                <td>Employee_Name</td>
                <td><form:input path="departmentId" /></td>
                <td><input type="submit" name="method" value="FindByDeptNO" /></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><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></td>
            </tr>
        </table>
    </form:form>
</body>
</html>
4

2 回答 2

2

作为基于单击的按钮为同一表单配置不同端点的替代方法(使用 HTML 中的单独表单或通过 JS 更改表单操作),您可以使用注释的params属性RequestMapping将表单提交进一步缩小到特定的控制器方法基于按钮的值(或任何其他表单提交字段)。有关更多详细信息,请参阅Spring 文档。

使用此策略,您的请求映射将如下所示:

@RequestMapping(value = "/employeeSearch", method = RequestMethod.POST, params="method=FindByName")
public ModelAndView searchByEmployeeName(
        @ModelAttribute("employeeForm") EmployeeForm employeeForm) {
...

@RequestMapping(value = "/employeeSearch", method = RequestMethod.POST, params="method=FindByDeptNO")
public ModelAndView searchByDeptId(
        @ModelAttribute("employeeForm") EmployeeForm employeeForm) {
...

@RequestMapping(value = "/employeeSearch", method = RequestMethod.POST, params="method=FindById")
public ModelAndView searchByEmpNo(
        @ModelAttribute("employeeForm") EmployeeForm employeeForm)
        throws Exception {
...
于 2013-01-10T05:34:43.313 回答
0

我不知道如何将请求映射到具有不同提交按钮值的特定方法

以单独的形式进行每个搜索。每个表单映射到不同的控制器方法。像这样:

<form:form action="empById" method="post" commandName="searchBean">
    Employee_ID
    <form:input path="employeeNumber"  />
    <input type="submit" name="method" value="FindById" />
</form:form>
<form:form action="empByName" method="post" commandName="searchBean">
    Employee_Name
    <form:input path="firstName" />
    <input type="submit" name="method" value="FindByName" />
</form:form>
<form:form action="empByDeptNo" method="post" commandName="searchBean">
    Employee_Name
    <form:input path="departmentId" />
    <input type="submit" name="method" value="FindByDeptNO" />
</form:form>

现在不同的搜索请求将映射到正确的控制器方法。

我的 EmployeeId 输入字段默认值为 0 我不知道为什么会发生

在将 EmployeeForm 的实例添加到模型之前,请将其初始化为您希望在搜索页面中看到的默认值。

于 2013-01-08T03:14:23.653 回答