lundi 27 juin 2016

Attribute will not display on JSP using setattribute and requestdispatcher

Below I have my servlet using both the attribute and dispatcher methods, the setAttribute() param is an instance of my Employee class. On the jsp file I am getting no output on the page; also, I am not getting any exceptions. I have read on the naming conventions using getters for a Bean but I do not believe that this is the case in this instance? Any help would be appreciated.

  import org.sql2o.Connection;

    import com.mack.sales.tools.ConnectionManager;

    /**
     * Servlet implementation class Edit_Employee
     */
    @WebServlet("/json/edit/employee")

    public class Edit_Employee extends HttpServlet {
        private static final long serialVersionUID = 1L;

        /**
         * @see HttpServlet#HttpServlet()
     */
    public Edit_Employee() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String empid = request.getParameter("id");
        System.out.println(empid + " empid");
        Employee emp = null;

        try (Connection con = ConnectionManager.getSql2o().open()) {

            String editSql = "SELECT emp_id, emp_name, hire_date FROM employee " 
                                + "WHERE  emp_id = :empid";
            emp = con.createQuery(editSql)
                    .addParameter("empid", empid)
                    .addColumnMapping("emp_id", "empid")
                    .addColumnMapping("emp_name", "empName")
                    .addColumnMapping("hire_date", "hiredate")
                    .executeAndFetchFirst(Employee.class);

        } // catch(SQLException e) {
            // e.printStackTrace();
            // }
        System.out.println(emp);
        //String someObjectAsJson = new Gson().toJson(emp);
        request.setAttribute("emp", emp);
        request.getRequestDispatcher("/resources/employees/admin/editemployeedata.jsp")
        .forward(request, response);        

    } ////////////////// end if (u != null)

    // }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Edit Employee</title>
<!-- Bootstrap styling -->
<link rel="stylesheet"
href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Datatables styling -->
<link rel="stylesheet"
href="//cdn.datatables.net/1.10.10/css/jquery.dataTables.min.css">
</head>

<body>
<div class="container">
    <div class="jumbotron">
        <h1>Edit Employee</h1>
        <p>
            Current User:
            <shiro:principal />

        </p>

    </div>
</div>

<div class="container">

    <h2>User List</h2>
    <hr />

    <table>
        <tr>
            <td>
            <button id="btn_homemodal" class="btn btn-primary">Home</button>
            </td>
            <td>


        </tr>
    </table>
</div>

<!-- JS Follows -->
<!-- JQuery -->
<!-- JQuery -->
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">           </script>
<!-- Bootstrap -->
<script
 src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js">  </script>
<!-- DataTables (Generate Tables based on Json) -->
<script 
  src="//cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script>

<!-- Custom code -->

<!-- BEGIN-----------HOME BUTTON MODAL TO HOME PAGE ONCLCK------------------    ------------------------>
<script type="text/javascript">
 document.getElementById("btn_homemodal").onclick = function () {
    location.href =    "http://localhost:8080/shiroSecApp/resources/home/home.jsp";
    };
 </script>      
 <!-- END-----------HOME BUTTON MODAL TO HOME PAGE ONCLCK-------------------   -----------------------> 
    <table><tr> <td>${emp.empName}</td> </tr></table>

 <p>test1</p>
</body>

package com.mack.sales.employees;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Employee {


 private String strDate;
 private String empid;
 private String empName;
 private Date hireDate;

public void setEmpid(String id) {
    this.empid = id;
}
public String getEmpID() {
    return empid;
}
public void setEmpName(String cn) {
    this.empName = cn;
}

public String getEmpName() {
    return empName;
}

 public void setHireDate( ) {
     strDate= "1970-01-01";
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
        try {
             java.util.Date date = sdf.parse(strDate);
             java.sql.Date hireDate = new Date(date.getTime());
             System.out.println(hireDate);
         } catch (ParseException e) {

             e.printStackTrace();
         }
    }

 public void setHireDate(Date hd) {
     this.hireDate=hd;
 }


 public Date getHireDate() {
    return hireDate;
 }

public String toString(){
    return empid + " " + empName  + " " + hireDate;
}

}

Aucun commentaire:

Enregistrer un commentaire