0

我是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 的控制器代码,根据员工表中的 href 自动填充 updateEmployee.jsp 页面上的字段。

4

2 回答 2

0

你需要尝试这样的事情

公共类 UpdateController 实现控制器 { 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> 
于 2012-10-01T12:13:32.660 回答
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;
      }  
于 2012-10-04T06:40:34.717 回答