views:

84

answers:

1

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

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...

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

<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

A: 

You should consider re-working your controller. Your desired operations should contextually fall under a semantic identifier. For CRUD (create, read, update, delete) operations for a User entity, one my set up the RequestMappings like:

  • /user (root user index page, lists all users)
  • /user/add (create a new user)
  • /user/delete/{id} (delete a user by id)
  • /user/{id} (find a user by id)

At the controller level, you would accomplish this by:

@RequestMapping(value="/user")
public class UserController
{
    @RequestMapping(method=RequestMethod.GET)
    public String getListUsers() // default page that is resolved by /user
    {
        return "user/list"; // used by view resolver
    }

    @RequestMapping(value="/add.html" method=RequestMethod.GET)
    public String getAddUser(Model model)
    {
        //add person form backing entity to model
        return "user/add"; // used by view resolver to locate view
    }

    @RequestMapping(value="/add.html" method=RequestMethod.POST)
    public String postAddUser(@ModelAttribute person) // other objects for errors, etc.
    {
        // add person, validate person, etc.
        return "redirect:/user"; // redirects to index
    }
}

et cetera...you'd follow those patterns for the other URIs

Droo
Annotations cannot be used. framework has been designed to use xml config can you give me example using MultiActionController instead?
Gauls
What do you mean designed to use XML config? Annotations are configured via spring config as well. The above example is a multi action controller.
Droo