春天人口在jsp表格领域更新......

时间:2012-10-01 09:49:00

标签: spring hibernate jsp

我是Spring的新手可以请任何人解决我的问题....

我的employee.jsp页面是....

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core"%>


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>${message}</title>
    </head>
    <body>
        <h3>${message}</ h3>
        <br/>
        <table>
            <tr>
                <th> Employee Code </ th>
                <th> Name </ th>
                <th> Designation </ th>
                <th> Date of Joining </ th>
            </tr>
            <c:forEach items="${employee}" var="employe">
                <tr>
                    <td> <c:out value="${employe.employeeCode}"> </c:out> </td>
                    <td><a href="updateEmployee.htm"> <c:out value="${employe.fullName}"> </c:out> </a></td>
                    <td> <c:out value="${employe.designation}"> </c:out> </td>
                </tr>
            </c:forEach>
        </table>
    </body>
</html>

对应的控制器代码是.....

public class EmployeeController implements Controller {
    Session ses=null;

    @Override
    public ModelAndView handleRequest (HttpServletRequest hsr,
                                      HttpServletResponse hsr1) throws Exception {
        ModelAndView mv = new ModelAndView ("employee");
        String out = "List of Employees";
        try {
            Session session = HibernateUtil.getSessionFactory (). getCurrentSession ();
            session.beginTransaction ();
            List result = session.createQuery ("from Employee").list();
            mv.addObject ("employee", result);
            session.getTransaction (). commit ();
        } catch (Exception e) {
            e.printStackTrace ();
        }
        mv.addObject ("message", out);
        return mv;
    }


}

UpdateEmployee.jsp代码是....

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>New Contact </title>
        <!--<link rel="stylesheet" href="ui.all.css" type="text/css" media="screen" />
<script type="text/javascript" src="js1.js"></script> 
    <script type="text/javascript" src="js2.js"></script>-->
    </head>
    <body>
        <h4><a href="index.htm">home</a> </h4>
        <h3>New Contact</h3>
        <spring:nestedPath path="updateEmployee">          
           <form action="" method="post">
                Name
                <spring:bind path="employeeCode">
                    <input type="text" name="${status.expression}" value="${status.value}">
                </spring:bind> 
                    <FONT color="red"><form:errors

path="name" /></FONT>

               Address
                <spring:bind path="FullName">
                    <input type="text" name="${status.expression}" value="${status.value}">
                </spring:bind>

                    Email
                <spring:bind path="Designation">
                    <input type="text" name="${status.expression}" value="${status.value}">
                </spring:bind>




                <input type="submit" value="Register">

                <font color="red"><form:errors

path="name" /></font>

            </form>
        </spring:nestedPath>
    </body>

帮助我使用updatecontroller的控制器代码,根据employee表格中的href自动填充updateEmployee.jsp页面上的字段....

2 个答案:

答案 0 :(得分:0)

你需要尝试这样的事情

公共类UpdateController实现Controller {     Session ses = null;

@Override 
public ModelAndView handleRequest (HttpServletRequest hsr, 
                                  HttpServletResponse hsr1) throws Exception { 
    ModelAndView mv = new ModelAndView ("updateEmployee"); 
    int empId = Integer.parseInt(hsr.getParameter("id"));
    String out = "Update Employee"; 
    try { 
        Session session = HibernateUtil.getSessionFactory (). getCurrentSession (); 
        session.beginTransaction (); 
        Employee result = (Employee) session.createQuery ("from Employee where id =:id").setParameter("id", empId).uniqueObject(); 
        mv.addObject ("employee", result); 
        session.getTransaction (). commit (); 
    } catch (Exception e) { 
        e.printStackTrace (); 
    } 
    mv.addObject ("message", out); 
    return mv; 
}  

}

然后在updateEmployee.jsp中编写类似这样的内容

<html>       
    <head>       
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">       
        <title>New Contact </title>       
        <!--<link rel="stylesheet" href="ui.all.css" type="text/css" media="screen" />       
<script type="text/javascript" src="js1.js"></script>        
    <script type="text/javascript" src="js2.js"></script>-->       
    </head>       
    <body>       
        <h4><a href="index.htm">home</a> </h4>       
        <h3>New Contact</h3>       
        <spring:nestedPath path="updateEmployee">                 
           <form action="" method="post">       
                Name       
                <spring:bind path="employeeCode">       
                    <input type="text" name="${status.expression}" value="${employee.employeeCode}">       
                </spring:bind>        
                    <FONT color="red"><form:errors       

path="name" /></FONT>       

               Address       
                <spring:bind path="FullName">       
                    <input type="text" name="${status.expression}" value="${employee.FullName}">       
                </spring:bind>       

                    Email       
                <spring:bind path="Designation">       
                    <input type="text" name="${status.expression}" value="${employee.Designation}">       
                </spring:bind>       




                <input type="submit" value="Register">       

                <font color="red"><form:errors       

path="name" /></font>       

            </form>       
        </spring:nestedPath>       
    </body> 

答案 1 :(得分:0)

Spring还提供 formBackingObject ,这也很有用

  @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
         Employee emp=new Employee();
         String designation=null;
         String fullName=null;
         int empCode=(Integer.parseInt(request.getParameter("employeeCode")));
         try {
                Session session = HibernateUtil.getSessionFactory (). getCurrentSession ();
                session.beginTransaction ();
                Query query = session.createQuery ("from Employee where EmployeeCode="+empCode);
               Iterator it=query.iterate();
               while(it.hasNext())
               {
                    Employee e=(Employee)it.next();              
                    designation=e.getDesignation();            
                    fullName=e.getFullName();
               }
               emp.setEmployeeCode(empCode);        
               emp.setDesignation(designation);
               emp.setFullName(fullName);
               session.getTransaction (). commit ();
            } catch (Exception e) {
                e.printStackTrace ();
            }

        return emp;
      }