如何将添加按钮链接到控制器JAVA

时间:2018-11-26 18:54:41

标签: java html spring hibernate

我是Spring MVC的新手。我创建了一个简单的add employee表单,以将雇员信息添加到数据库(忽略UI)。 Simple add form to take data

当我单击添加按钮时,我想使用控制器方法addEmployee()进行映射,并在实体类AddEmployee.java的帮助下将数据发送到AddDao.java。所以我有点困惑,如何映射添加按钮添加控制器的Employee方法。

Controller method which should be called or mapped after clicking Add button

添加表单代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
 <html>
  <head>
   <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>Add Employee</title>
  </head>
  <body>
  <form:form id="addForm" modelAttribute="employee" action="addEmployee" method="post">
    <table align="center" width="20%" border="1" >

      <tr>
        <td>
         <form:label path="firstname">FirstName</form:label>
        </td>
         <td>
         <input type="text" placeholder="Enter First name" name="firstname" required>
         </td>
        </tr>


        <tr>
          <td>
            <form:label path="lastname">LastName</form:label>
          </td>
          <td>
            <input type="text" placeholder="Enter Last Name" name="lastname" required>
          </td>
         </tr>


         <tr>
           <td>
              <form:label path="email">Email</form:label>
           </td>
           <td>
              <input type="text" placeholder="Enter Email" name="email" required>
           </td>
           </tr>

          <tr>
            <td>
               <form:label path="address">Address</form:label>
            </td>
            <td>
               <input type="text" placeholder="Enter Address" name="address" required>
            </td>
           </tr>

           <tr>
            <td>
               <form:label path="age">Age</form:label>
            </td>
            <td>
                <input type="text" placeholder="Enter Age" name="age" required>
            </td>
           </tr>

            <tr>
               <td>
                 <form:label path="phone">Phone</form:label>
               </td>
               <td>
                 <input type="text" placeholder="Enter Phone" name="phone" required>
               </td>
             </tr>

            <tr>
            <td colspan="2">

                <input type="submit" name="add" value="Add"  formaction="/addemp" />
            </td>
            </tr>

                <tr></tr>
             <tr>
                <td></td>
                 <td><a href="index.jsp">Home</a>
              </td>
             </tr>

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

控制器代码:

   package com.roshan.empl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.roshan.empl.dao.AddDao;
import com.roshan.empl.entity.AddEmployee;

@Controller
public class HomeController {


    AddEmployee add;

    @Autowired
    AddDao adao;

    @RequestMapping("/home")
    public String home() {

        return "index.php";
    }

   @RequestMapping("/addemployee")
    public String add()
     {

        return "addemployee.jsp";
    }



   @RequestMapping("/search")
    public String search()
     {

        return "search.jsp";
    }

     @RequestMapping(" ")
     public String addEmployee(@RequestParam("firstname")String firstname,@RequestParam("lastname")String lastname,@RequestParam("email")String email,@RequestParam("address")String address ,@RequestParam("age")int age,@RequestParam("phone")String phone)

     {

    add = new AddEmployee(firstname,lastname,email,address,age,phone);
    adao.addEmployee(add);
    return "index.jsp" ;

     }
     }

1 个答案:

答案 0 :(得分:0)

您必须在表单的action属性上创建一个Endpoint作为方法的路径,例如:

<form action="/addEmploye" method="POST">

并将其映射到您的方法,您应该将注释添加到您的方法中,例如:

 @ResquestMapping(value="/addEmploye" method=RequestMethod.POST)

就是这样。