Announcement Announcement Module
Collapse
No announcement yet.
SimpleFormController Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • SimpleFormController

    Hi there

    I am having issue with SimpleFormController.
    I have this code from a tutorial but I think it is quite old.
    I am using Spring MVC, and I want to display an object details on a "update Page". I m using a spring:bind to diplay the details
    But on my page the ${status.expression} is not evaluated so my input field value is ${status.expression}.

    Jsp
    Code:
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
    <%@ taglib prefix="spring" uri="http://www.springframework.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>Time Expression - Enter Hours</title>
    </head>
    
    
    <form method="post">
    <table width="85%" border="1" align="center" cellpadding="0"
        cellspacing="0">
      <tr>
        <td>
        <table width="100%" border="0" align="center" cellpadding="4"
            cellspacing="0" bordercolor="#CCCCCC">
          <tr bgcolor="#C2DCEB" valign="middle">
              <td width="90%" height="60">
              <h1>Update Timesheet <c:out value="${command.timesheetid.expression}" /></h1>
              </td>
              <td align="right" nowrap="nowrap"><a href="signout.htm">Sign out</a></td>
          </tr>
          <tr>
            <td colspan="2" align="center">
            <center><spring:bind path="command.*">
                <c:if test="${not empty status.errorMessages}">
                    <c:forEach var="error" items="${status.errorMessages}">
                        <font color="red"><c:out value="${error}" escapeXml="false" /> </font>
                        <br />
                    </c:forEach>
                </c:if>
            </spring:bind> <!-- status messages --> <c:if
                test="${not empty message}">
                <font color="green"><c:out value="${message}" /></font>
                <c:set var="message" value="" scope="session" />
            </c:if></center>
            </p>       
            <p>List
            <spring:bind path="command.*">
                    <c:forEach var="name" items="${status.expression}">
                        <font color="red"><c:out value="${name}"/> </font>
                        <br />
                    </c:forEach>
            </spring:bind>
            </br>...</p>
            <table border="1" align="center" cellpadding="8" cellspacing="0">
             <tr>
                 <th>#</th>
                 <th>Teacher</th>
                 <th>Booking</th>
                 <th>WeekStart</th>
                 <th>Total</th>
             </tr>
             <tr>           
               <td><spring:bind path="command.timesheetid">
                   <input name='<c:out value="${status.expression}"/>'
                       value='<c:out value="${status.value}"/>' type="text" size="4"
                       maxlength="6">
               </spring:bind></td>
               <td><spring:bind path="command.teacherid">
                   <input name='<c:out value="${status.expression}"/>'
                       value='<c:out value="${status.value}"/>' type="text" size="4"
                       maxlength="6">
               </spring:bind></td>
               <td><spring:bind path="command.bookingid">
                   <input name='<c:out value="${status.expression}"/>'
                       value='<c:out value="${status.value}"/>' type="text" size="4"
                       maxlength="6">
               </spring:bind></td>
               <td><spring:bind path="command.weekstart">
                   <input name='<c:out value="${status.expression}"/>'
                       value='<c:out value="${status.value}"/>' type="text" size="4"
                       maxlength="6">
               </spring:bind></td>
               <td><spring:bind path="command.days_worked">
                   <c:out value="${status.value}" />
               </spring:bind></td>
              </tr>
            </table>
            <p align="center"><input name="save" type="submit" value="Save"> <input
                name="cancel" type="button" value="Cancel"
                onClick="javascript:window.location='timesheetlist.htm'"> <br>
            <br>
            </p>
            </td>
          </tr>
          <tr>
              <td colspan="2" bgcolor="#C2DCEB">&nbsp;</td>
          </tr>
        </table>
        </td>
      </tr>
    </table>
    </form>
    <p>&nbsp;</p>
    </body>
    </html>
    Controller
    Code:
    package com.mycompany.controller;
    
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.beans.propertyeditors.CustomDateEditor;
    import org.springframework.validation.BindException;
    import org.springframework.web.bind.ServletRequestDataBinder;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.SimpleFormController;
    
    import com.mycompany.dao.TimesheetDAO;
    import com.mycompany.domain.Timesheet;
    
    @SuppressWarnings("deprecation")
    public class TimesheetUpdateController extends SimpleFormController {
    
    	private TimesheetDAO timesheetDAO;
    	public static final String TID = "tid";
    
    	public TimesheetUpdateController(){
    		setCommandName("command");
    		setCommandClass(Timesheet.class);
    	}
    	
    	public void setTimesheetDAO(TimesheetDAO timesheetDAO) {
    		this.timesheetDAO = timesheetDAO;
    	}
    
    	protected Object formBackingObject(HttpServletRequest request) {
    		System.out.println("------formBackingObject");
    
    		if (request.getParameter(TID) != null
    				&& request.getParameter(TID).trim().length() > 0){
    		// -->TIMESHEET EXIST
    			System.out.println("--Exists");
    			
    			return timesheetDAO.loadTimesheet(Long.parseLong(request
    					.getParameter(TID)));
    		}
    		// --> NEW TIMESHEET
    		Timesheet timesheet = new Timesheet();
    		/*
    		 * Employee employee = (Employee) applicationSecurityManager
    		 * .getEmployee(request);
    		 * timesheet.setEmployeeId(employee.getEmployeeId());
    		 */
    		timesheet.setCreatedby(99);
    		System.out.println("--New");
    
    		return timesheet;
    	}
    
    	/**
    	 * Returns Hashmap containing a list of all Department database records
    	 * 
    	 * @see com.visualpatterns.timex.model.Department
    	 */
    /*	protected Map referenceData(HttpServletRequest request) throws Exception {
    		HashMap model = new HashMap();
    		model.put("departments", departmentManager.getDepartments());
    
    		return model;
    	}
    */
    	/**
    	 * Saves Timesheet command object using
    	 * timesheetManager.saveTimesheet(timesheet);
    	 * 
    	 * @see TimesheetManager
    	 */
    	protected ModelAndView onSubmit(HttpServletRequest request,
    			HttpServletResponse response, Object command, BindException errors) {
    		System.out.println("------SUBMIT");
    
    		Timesheet timesheet = (Timesheet) command;
    		timesheetDAO.saveTimesheet(timesheet);
    		request.getSession().setAttribute(
    				"message",
    				getMessageSourceAccessor().getMessage(
    						"message.enterhours.savesuccess"));
    		return new ModelAndView(getSuccessView());
    	}
    
    	protected void initBinder(HttpServletRequest request,
    			ServletRequestDataBinder binder) throws Exception {
    		System.out.println("------BIIINNNNNDDER");
    		DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
    		CustomDateEditor editor = new CustomDateEditor(df, false);
    		binder.registerCustomEditor(Date.class, editor);
    	}
    }
    conf
    Code:
    <bean name="timesheetUpdateController" class="com.mycompany.controller.TimesheetUpdateController">
    		<property name="sessionForm">
    			<value>true</value>
    		</property>
    		<property name="timesheetDAO">
    			<ref bean="myTimesheetDAO" />
    		</property>
    		<property name="successView">
    			<value>timesheetlist</value>
    		</property>
    	</bean>
    How come the expression are not evaluated? Did I forget something??

    Please help

  • #2
    Change your web.xml from version 2.3 to version 2.4... Also make sure you have included the proper jstl jars inyour app.

    Comment


    • #3
      isELIgnored

      Ok I ll change my web.xml

      I have all the correct jar and everything.

      I fixed this issue by myself by adding this line in my jsp file

      Code:
      <%@ page isELIgnored="false"%>
      It is strange I have to set this parameter to false I guessed it would be by default true. Maybe I can update this in my web.xml or spring conf file?

      Thanks anyway

      Comment


      • #4
        By the web this is my web.xml and I think it is 2.5 already
        Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        	xmlns="http://java.sun.com/xml/ns/javaee" 
        	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        	id="WebApp_ID" version="2.5">

        Comment

        Working...
        X