Announcement Announcement Module
Collapse
No announcement yet.
Request parameter is null calling methods on MultiActionController Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Request parameter is null calling methods on MultiActionController

    In Spring 3.0

    if i have a jsp page with two different links each calling different method on MultiActionController

    <form:form method="POST">
    <a href="user.htm?action=add" style="color: blue;">Add</a>
    <a href="user.htm?action=delete" style="color: blue;">Delete</a>
    </form:form>
    on the MultiActionController i get request parameter as null so cannot validate the values

    String username=request.getParameter("username");
    String password=request.getParameter("password");
    i also tried uing a button and changed it to look like a link but in case of button click add method is called twice once with requestparameter as null and second time with correct values, but this twice entry is creating a mess in the code also to make this work i am using form action which will not work in case of two different method calls

    <form:form action="user.htm?action=add method="POST">
    <input type="submit" value="I have info"/>"> ---> should call delete method
    <input type="submit" value="Click to send info"/> ---> should call add method

    </form:form>
    want to achieve this without javascript

    I have also set param reslover in xml file with default method to call

  • #2
    I think you are a lot confused about j2ee, http requests, forms, Spring binding and the whole logic behind mvc...

    First of all, logically speaking a form can only have one submit (it doesn't make sense to use 2 input:submit in a form because they will do the same thing:submit the form...the second code you posted just makes me shudder).

    Second: you are speaking about an add and a delete, then you speak about username and password...are you trying to write a login page, or a page with a form to add a user? In the first case, you should delete no thing at all, in the second, only the add button should be bound to the form, since deletion isn't logically bound to a form but to a single value (i.e. user id, or username) and the methods to handle the 2 things should be completely separated.

    Third, you don't know how to use the spring form tags properly. The form:form tag shouldn't have an "action" or a "method" attribute but a "commandName" instead; this is used to bind your form object to your controller...just read the documentation about spring tags, I can't explain it all here.

    In short, my suggestion is you study more j2ee, requests, Spring and Spring web mvc, and get a clearer idea of your business flow before you start writing down code...

    Comment


    • #3
      Hi

      let me explain my problem again forget the above code i was just giving some example

      I have a jsp page which has two input text field and two links each should call different method of the controller both the method will validate the input and will get redirect to another page simple!!


      The reason i have using MultiActionController.......
      Unfortunately i have to continue using a controller which extends MultiActionController because the same jsp page also has paging which work absolutely fine


      So all i wan to do is simply achieve server and client side validation once either of the link is clicked and redirect accordingly.

      Please give me some example to move ahead in this...

      Comment


      • #4
        Then simply you can't use a form, because one form is bound to one action / one object. Don't use the form, but use two buttons with two onclick that go to two different javascript functions. In each function, retrieve the fields data and append them to the string of the url that navigates to each controller method, then navigate to the method and retrieve the data in the controller using the request...

        ...or switch to Spring 3's annotated controllers and save yourself a lot of trouble.

        Comment


        • #5
          Hi can you give me an example?

          Is there any other way without javascript?

          Comment


          • #6
            jsp:

            Code:
            <input type="text" id="uname" size="15"/>
            <input type="password" id="pswd" size="15"/>
            
            <input type="button" value="Add" onclick="goToAddPage()"/>
            <input type="button" value="Delete" onclick="goToDelPage()"/>
            javascript:

            Code:
            function goToAddPage() {
              window.location = "URL-to-add-method" + "?uname=" + $('uname').value + "&pswd=" + $('pswd').value;
            }
            
            function goToDelPage() {
              window.location = "URL-to-delete-method" + "?uname=" + $('uname').value + "&pswd=" + $('pswd').value;
            }
            and, in your controller, in both methods retrieve the values with:

            Code:
            String userName = request.getAttribute("uname");
            String password = request.getAttribute("pswd");

            Comment


            • #7
              hello

              im new here
              thanks for the info..


              http://www.townsrealty.com

              Comment


              • #8
                Thanks for the example

                since i want to achieve this w/o javascript i got this example here
                but my problem is why the requestParameter is Null

                http://www.goospoos.com/2009/11/spri...oller-example/


                Here's my code

                Code:
                <bean id="myExampleController" class="com.ui.controller.MyExampleController">
                			<property name="methodNameResolver">
                			<ref bean="paramResolver" />
                		</property>	
                		<property name="validators" ref="myExampleValidator"/> 		 
                	</bean>
                		
                	<bean id="paramResolver"
                		class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
                <property name="defaultMethodName" value="loadPage"></property>	 
                <property name="paramName">
                <value>action</value>			 
                </property>		
                </bean>
                
                <bean id="myExampleValidator" class="com.validator.MyExampleValidator" />

                Controller

                public ModelAndView validateValues(HttpServletRequest request, HttpServletResponse response) throws Exception{
                ModelAndView mav=null;
                ----> this is null ???
                String value1=request.getParameter("textvalue1");
                String value2=request.getParameter("textvalue2");


                mav = new ModelAndView("myexample");
                mav=getPageData(request, false);
                return mav;

                }


                JSP page

                <form action="myexample.htm" method="post">

                input type="text" name="textvalue1" size="20" />
                input type="text" name="textvalue2" size="20" />
                </form>

                <a href="myexample.htm?action=validateValues">click to validate</a>
                ----------> what's wrong with this if the above mentioned site can call a method and works fine ,why i cannot get request parameters
                Last edited by gauls; Sep 6th, 2010, 10:32 AM.

                Comment


                • #9
                  String value1=request.getParameter("textvalue1");
                  String value2=request.getParameter("textvalue2");
                  mav = new ModelAndView("myexample");
                  mav=getPageData(request, false);
                  1) are you aware that lines 1-3 in this code are perfectly useless and could be dropped?

                  2) what are you doing in your getPageData() method? Because that is all that matters inside your controller...your model and logical view are constructed there from scratch...

                  <form action="myexample.htm" method="post">

                  input type="text" name="textvalue1" size="20" />
                  input type="text" name="textvalue2" size="20" />
                  </form>

                  <a href="myexample.htm?action=validateValues">click to validate</a>
                  1) Why including the inputs inside a form, if you're not going to ever submit it?

                  2) (That is the source of your null params in request) look at your href: you are NOT including the input values in the URL (that is what my javascript functions where for)!!! How can you expect to find request parameters if you don't include them in the request itself???

                  You REALLY need a good study of j2ee, http protocol, and the whole web app concept...trust me...

                  Comment


                  • #10
                    Hi

                    The first few lines within the controller is used to check if request has some values if yes then bind and validate bean. everything works fine on the jsp page navigation and input box validation

                    I have used two submit buttons and made it look like links using css both calls same method which binds the request to bean and validates the values
                    WebUtils.hasSubmitParameter is used to differentiate which button was clicked.

                    Here's the code
                    jsp page

                    Code:
                    <form:form action="myexample.htm?action=validateValues"
                    	 method="POST">
                    <input type="text" name="text1" size="20" />
                    <form:errors path="text1" />
                    <input type="submit" name="add" style="cursor: pointer" value="Add">
                    
                    <input type="text" name="text2" size="20" />
                    <form:errors path="text2" />
                    <input type="submit" name="Edit" style="cursor: pointer" value="Edit">
                    Controller

                    Code:
                    public class MyExampleController extends MultiActionController{
                    BindingResult errors;
                    
                    	public BindingResult getErrors() {
                    		return errors;
                    	}
                    
                    	public void setErrors(BindingResult errors) {
                    		this.errors = errors;
                    	} 
                    
                    protected void bind(HttpServletRequest request, Object command) throws Exception {		 
                    ServletRequestDataBinder binder = createBinder(request, command); 
                    		binder.bind(request); 
                    		errors = binder.getBindingResult(); 
                    		binder.closeNoCatch();//considers all errors fatal
                    	}
                    
                    public void validate(Object command) {
                    		Validator[] validators = getValidators(); 
                    		if (validators != null){ 
                    			for(int index = 0; index < validators.length; index++) {
                    				Validator validator = validators[index]; 
                    				if(validator instanceof MyExampleValidator) { 
                    					if(((MyExampleValidator) validator).supports(command .getClass())) {
                    						ValidationUtils.invokeValidator(validators[index], command,errors); 
                    					} 
                    				}else if (validator.supports(command.getClass())) {
                    					ValidationUtils.invokeValidator(validators[index], command, errors); 
                    				} 
                    			} 
                    		} 
                    	}
                    
                    public ModelAndView validateValues(HttpServletRequest request, HttpServletResponse response) throws Exception{
                    ModelAndView mav=null;
                    MyExampleForm myExampleForm = (MyExampleForm) newCommandObject(MyExampleForm.class);
                    
                    bind(request, myExampleForm);
                    
                    //client side validation
                    validate(myExampleForm); 
                    
                    if (errors.hasErrors()) {
                    mav=getPageData(request,false); --> gets look ups 	 
                    mav.addAllObjects(errors.getModel());--> this is what loads <forms:errors path="text1">
                    return mav;
                    	}
                    }
                    
                    //server side validation
                    ....................
                    
                    //all above is fine then
                    //check which button was clicked and redirect to different url WebUtils.hasSubmitParameter(request,"add");
                    }
                    config file

                    Code:
                    <bean id="myExampleValidator" class="com.validator.MyExampleValidator" />
                    
                    <bean id="myExampleController" class="com.controller.MyExampleController" >
                    <property name="methodNameResolver">
                    <ref bean="paramResolver" />
                    </property>	
                    <property name="validators" ref="myExampleValidator"/> 		 
                    </bean>
                    
                    <bean id="paramResolver"
                    class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
                    <property name="defaultMethodName" value="loadPage"></property>
                    <property name="paramName">
                    <value>action</value>			 
                    </property>		
                    </bean>
                    everything works fine now w/o javascript
                    i will be trying to move paging to annotations based whenever time permits

                    Further questions are

                    1. When trying to use
                    <form:form action="myexample.htm?action=validateValues"
                    method="POST" CommandName="command"> --> also tried myExampleForm/MyExampleForm
                    <form:input path="text1"> -> binding error occurs
                    but <form:errors path="text1"> works fine with/without CommandName="command". Don't want to use old spring:bind

                    2. With my current example jsp page shows errors but user input values are disappered, how do i retain it?

                    Comment

                    Working...
                    X