Results 1 to 2 of 2

Thread: Creating custom JSP Tags & passing spring controller data to them.

  1. #1

    Default Creating custom JSP Tags & passing spring controller data to them.

    I have a Controller which returns a custom object of type Course:
    Code:
    return new ModelAndView( getSuccessView(), "course", myCourseObject );
    Then, in my JSP view, I try to output it by:
    Code:
    <courses:courselist coursesVar="${course}"></courses:courselist>
    Here is my tld file:
    Code:
    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
                               "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
    <taglib>
        <tlibversion>1.0</tlibversion>
        <jspversion>1.1</jspversion>
        <shortname>courses</shortname>
        <info>An tag library for use with the Course Registration System</info>
        <tag>
            <name>courselist</name>
            <tagclass>courses.util.CourseListingTag</tagclass>
            <info>Prints a listing of courses in a pretty table</info>
            <attribute> 
                <name>coursesVar</name>
                <required>true</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
        </tag>
    </taglib>
    Then, in my actual tag, I am attempting to output information about this object:
    Code:
    @Override
    	public int doStartTag() throws JspException {
    		logger.debug( "doStartTag -- with coursesVar= "+ coursesVar );
    		
    		StringBuffer buffer = new StringBuffer();
    		
    		Object obj = ExpressionEvaluationUtils.evaluate("coursesVar", this.coursesVar, Course.class, pageContext);
    		if( obj == null ) { buffer.append( "courses attribute null"); }
    		else { buffer.append( "courses attribute not null"); }
    		buffer.append( "<br>obj: " + obj + " as: " + obj.getClass()+ "<br>" );
    		
    		try {
    			pageContext.getOut().println( buffer.toString() );
    		} catch (IOException e) {
    			logger.error( "IOException in CourseListingTag!");
    			throw new JspException( "IOException when starting courselisting tag", e );
    		}
    		return EVAL_BODY_INCLUDE;
    	}
    Trying to access this controller, I receive this exception:
    Code:
    javax.servlet.ServletException: Attribute value "courses.model.entities.Course@1917a08" is neither a JSP EL expression nor assignable to result class [courses.model.entities.Course]
    Eventually, once I figure out how to do this, I want to make it so I can use my couses:courselisting tag to output a set of Course objects retrieved from Hibernate in a nice table.

    Any ideas on how to make this work? Thanks

  2. #2

    Default

    Problem Solved
    I just decided to not use the ${} syntax anymore. I just directly pass in the string name of the attribute:
    Code:
    <courses:courselist coursesVar="courses"/>
    Then, I retrieve it by using this:
    Code:
    Object o = pageContext.findAttribute( coursesVar );

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •