【问题标题】:How to map input value from form to URL of findById()如何将输入值从表单映射到 findById() 的 URL
【发布时间】:2016-06-16 14:03:11
【问题描述】:

/welcome/employees

I have a input area where user enters `"empNo"` and clicks on "Find Employee", mapping to be done to /welcome/employees/find/{id}.

[`it should insted be move to url appended with empNo`][2]

这里一切正常,我直接输入网址,它放弃了结果。

但“查找员工”按钮未映射到输入的 /welcome/employees/find/{id},而是映射到 /welcome/employees/find/。

以及如何将输入的 emp No 映射到 Find Employee 按钮。请说明一下。

仅当我手动输入 URL 时,控制器获取员工方法才能正常工作。

 list.jsp form page


    <input type="text" path="empNo" value="${employee.empNo}" placeholder="Employee No."/>${employee.empNo}
    &nbsp;
    <a href="/welcome/employees/find/${employee.empNo}" class="btn btn-primary">
      <span class="glyphicon glyphicon-search"></span> Find Employee
        </a>




 find.jsp 


    <h2>Employee information</h2>
    <form action="/welcome/employees/find/${employee.empNo}">
    <table class="table table-bordered">
      <tr>
            <th>Employee No.</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Gender</th>
            <th>Birth Date</th>


<th>Hire Date</th>
  </tr>
  <tr>
        <td>"${employee.empNo}"</td>
        <td>"${employee.firstName}"</td>
        <td>"${employee.lastName}"</td>
        <td>"${employee.gender}"</td>
        <td>"${employee.birthDate}"</td>
        <td>"${employee.hireDate}"</td>
 </tr>
  </table>
</form>

返回

@RequestMapping(value = "/employees/find/{empNo}", method = RequestMethod.GET)
public String getEmployee(@PathVariable("empNo") long empNo, Model model){
    Employee employee1=this.employeeService.findEmployeeById(empNo); 
    model.addAttribute(employee1);
    return "employees/find";
}

当我点击查找员工时,它会指向

http://localhost:8080/welcome/employees/find/

【问题讨论】:

  • 这是没有发生的映射,如果我手动输入带有 {empNo} 的 URL,“查找员工”按钮不会将我带到带有 {empNo} 的 URL,它似乎工作正常。
  • 我认为${employee.empNo}find.jsp 页面没有任何价值。检查${employee.empNo} 给你任何东西。
  • find.jsp 似乎很好,因为使用 empNo 手动键入 url 可以正常工作,但使用 empNo 的 url 未映射到 list.jsp 中的表单。我不确定如何让用户输入 empNo 映射到 url 从表单到按钮,然后到 url
  • 我在 find.jsp 中看不到 submit button。如果您不提交任何数据,为什么不直接使用&lt;a&gt; tag

标签: java html spring jstl


【解决方案1】:

这是没有任何 javascript 的简单方法。

listing.jsp

<form action="/welcome/employees/find" method="get"> 
    <input name="empNo" placeholder="Employee No."/> 
    <button type="submit">Find Employee</button> 
</form>

说明:当您执行${employee.empNo} 时,您从employee 对象内的empNo 变量中获取值。但是你不想用这个值来查找。您想使用文本框中的值来查找。

控制器

@RequestMapping(value = "/employees/find", method = RequestMethod.GET)
public String getEmployee(@RequestParam("empNo") long empNo, Model model){
    Employee employee1=this.employeeService.findEmployeeById(empNo); 
    model.addAttribute(employee1);
    return "employees/find";
}

在 find.jsp 中不需要&lt;form&gt;,除非你想做点什么。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-21
  • 2012-11-07
  • 2011-04-03
相关资源
最近更新 更多