Прежде всего, я новичок в весеннем mvc.Мне запутался в отображении всех записей сотрудников из базы данных, которая соответствует имени сотрудника.
Я попытался получить имя сотрудника из текстового поля в SearchControllerкак показано ниже, и отправить его в класс сущности AddEmployee для правильного присвоения. После правильного присвоения имени, я отправляю его методу getallEmployee класса AddDao.
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.roshan.empl.dao.AddDao;
import com.roshan.empl.entity.AddEmployee;
import com.roshan.empl.service.EmployeeService;
@Controller
public class SearchController {
@Autowired
AddEmployee add;
@Autowired
AddDao adao;
@RequestMapping("/search")
public String search()
{
return "search.jsp";
}
@RequestMapping(value= "/searchEmployee" , method=RequestMethod.POST)
public String searchEmployee(@RequestParam("firstname") String firstname)
{
add = new AddEmployee(firstname);
adao.getAllEmployee(add);
return "" ;
}
В классе AddDao getAllEmployee () возвращает все соответствующие записиИмя сотрудника.
package com.roshan.empl.dao;
import javax.transaction.Transactional;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.roshan.empl.entity.AddEmployee;
import antlr.collections.List;
@Component
public class AddDao {
@Autowired
private SessionFactory sessionFactory;
@Transactional
public void addEmployee(AddEmployee ad) {
System.out.println("in addemployee" +ad);
Session session = sessionFactory.getCurrentSession();
session.save(ad);
}
//public AddEmployee getEmployee(String firstname) {
// return(AddEmployee) sessionFactory.getCurrentSession().get(AddEmployee.class,firstname);
//}
public List getAllEmployee(AddEmployee firstname) {
String hql ="FROM EMPLOYEE E WHERE E.firstname = firstname";
return (List) sessionFactory.getCurrentSession().createQuery("from Employee").list();
}
}
Но мне интересно, как я могу сохранить эти совпадающие записи в SearchaController (возможно, используя ArrayList, LinkedList) и отправить эти записи в соответствующие столбцы таблицы в представлении serach.jsp.
<%@ 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> Search Employee </title>
</head>
<body>
<form action="/searchEmployee">
<tr>
<td>
<input type="text" placeholder="Search by First Name" name="firstname" required>
</td>
<td colspan="2">
<input type="submit" value="Search Employee">
</td>
</tr>
</form>
<br>
<br>
<tr>
<td></td>
<td><a href="index.jsp">Home</a>
</td>
</tr>
<br>
<br>
<br>
<table border="1">
<th>ID</th>
<th>First name</th>
<th>Last name</th>
<th>Email</th>
<th>Address</th>
<th>Age</th>
<th>Phone</th>
<c:forEach items="$ " var=" ">
<tr>
<td></td>
<td></td>
<td></td>
<td> </td>
<td></td>
<td> </td>
<td></td>
</tr>
</c:forEach>
</table>
</body>
</html>