Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: command name/class defined - why can't my jsp see it?

  1. #1
    Join Date
    Mar 2007
    Posts
    27

    Default command name/class defined - why can't my jsp see it?

    Hello,

    I'm having some trouble binding to my command object with spring:bind in my jsp view. Here is the relevant code form my servlet.xml:

    Code:
    <bean id="stateController" class="springdev.StateController">
            <property name="sessionForm" value="true" />
            <property name="commandName" value="state" />
            <property name="commandClass" value="bus.State" />
            <property name="formView" value="selectstate" />
    The "State" object looks like this:

    Code:
    public class State implements Serializable {
        private List<County> counties;
        private String name;
    
        public List<County> getCounties() {
            return counties;
        }
    
        public void setCounties(List<County> counties) {
            this.counties = counties;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        } ...
    And I am trying to bind to that State state object like so:

    Code:
    <spring:bind path="state.name">
    Yet I am getting this error:
    org.apache.jasper.JasperException: Neither Errors instance nor plain target object for bean name 'state' available as request attribute

    I've seen a few threads here which touch on similar issues people have, but they seem to involve more sophisticated usage of the bind tags. Personally I find the spring tag references to be lacking in exposition and code example, and as a result I might not be using the tags properly.

    I think this is enough code to make sense of it (assume that my viewResolvers and web.xml are configured properly), but if you need more I'll be happy to provide it. Thanks!

  2. #2

    Default

    Can you post your controller and full jsp code?

    It'll be of use trying to understand your environment.
    Leonardo Borges

  3. #3
    Join Date
    Mar 2007
    Posts
    27

    Default

    Quote Originally Posted by leonardoborges View Post
    Can you post your controller and full jsp code?

    It'll be of use trying to understand your environment.
    Here it is - fairly simplistic as I'm trying more to understand how binding at the view level interacts with Spring, especially with regard to how the command object gets populated.

    First, a couple assumptions on my part, please correct them if they are wrong:
    1) I should not specify a pre-existing bean as my command object in my servlet.xml specification of the controller bean
    2) the bean specification of the controller will instantiate the command object that will be bound to by the form view
    3) formBackingObject does not necessarily need to be overridden in a SimpleFormController subclass like the one here.

    Thanks for your help!

    Code:
    public class StateController extends SimpleFormController {
        private StateAggregate stateAggregate;
    
        @Override
        protected Map referenceData(HttpServletRequest request) throws Exception {
            HashMap map = new HashMap();
            map.put("myStates", getStateAggregate());
            return(map);
        }
    
        @Override
        protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException bindEx) throws Exception {
            State input = (State) command;
            if(isFormSubmission(request)) {
                Iterator<State> i =  getStateAggregate().getStates().iterator();
                while(i.hasNext()) {
                    State s = i.next();
                    if(s.getName().equalsIgnoreCase(input.getName()))
                        input.setCounties(s.getCounties());
                }
            }
            command = input;
            return new ModelAndView(getSuccessView());
        }
     }

  4. #4

    Default

    Ok, can I see your full jsp code also?
    Leonardo Borges

  5. #5

    Default

    Also which Spring version you're using?
    Leonardo Borges

  6. #6
    Join Date
    Mar 2007
    Posts
    27

    Default

    Quote Originally Posted by leonardoborges View Post
    Ok, can I see your full jsp code also?
    I am using Spring 1.2. Here's my view.

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

    Code:
    <!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">
    <head><title>Select a State</title></head>
    <body>
    <h2>Select your state:</h2>
    <spring:bind path="state.name">
    <form method="post">
    <select name="<c:out value="${status.expression}" />" >
        <c:forEach items="${myStates.states}" var="state">
            <option value="<c:out value="${state.name}" />" />
                <c:if test="${status.value} == ${state.name}">selected</c:if>
                <c:out value="${state.name}"/>
                </option>
                </c:forEach>
                </select>
    </spring:bind>
    <input type="submit" value="Go">
    </form>
    <hr/>
    
        <c:if test="${!empty state.name}">
            <h2><c:out value="${state.name}"/> Counties</h2>
        </c:if>
    
    
    </body>
    </html>

  7. #7

    Default

    I'm not quite familiar with this version of spring.

    Is there any restrictions for using the new 2.0 version?
    In this version spring has a form taglib (http://www.springframework.org/docs/...mvc-formtaglib) that I find more powerful, and is working great for situations like yours.
    Leonardo Borges

  8. #8
    Join Date
    Mar 2007
    Posts
    27

    Default

    Unfortunately there is such a restriction. I've seen the taglib for 2.0 and it's very enticing! I could probably solve my problem with it but I am stuck with 1.2 for the time being.

  9. #9

    Default

    That's not nice!

    Well, I'm not really sure how different is the use of spring:bind andthe new taglib. Perhaps, if someone who worked with this version could step in and give an opinion.
    Leonardo Borges

  10. #10
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default

    That looks absolutely fine to me - is it possible that your controller isn't mapped correctly to that page? How are you accessing the page (what URL are you providing), and please post the parts of your Spring config related to mapping and view resolution.
    Peter Mularien | Blog
    Author, Spring Security 3 (Book) - Packt Publishing, Available in print and eBook form
    SCJP 5, Oracle DBA
    Any postings are my own opinion, and should not be attributed to my employer or clients.


Posting Permissions

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