aspose file tools*
The moose likes Other Application Frameworks and the fly likes how to  tell  MultiActionController Command Object is Of type com.dss.ems.model.User Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "how to  tell  MultiActionController Command Object is Of type com.dss.ems.model.User" Watch "how to  tell  MultiActionController Command Object is Of type com.dss.ems.model.User" New topic
Author

how to tell MultiActionController Command Object is Of type com.dss.ems.model.User

ganesh pol
Ranch Hand

Joined: Apr 29, 2005
Posts: 151
hi friends
normally in spring's simple form controller we are registering one command object for one controller
like following code

<beanid="loginController"
class ="com.dss.ems.controllers.LoginController">
<property name="sessionForm"><value>true</value></property>
<property name="commandName"><value>usermodel</value></property>
<property name="commandClass"><value>com.dss.ems.models.UserModel</value></property>



but in multiaction controller how should we proceed to register one command object for one method


i will give u one example


my Controller code is


public class LoginController extends MultiActionController{
private Map userMap=null;

public LoginController() {
System.out.println(" In LoginController ");
}

public ModelAndView showLoginPage(HttpServletRequest request,
HttpServletResponse response,
Object obj) throws ServletException {
System.out.println("i am giving login page");
ModelAndView mav=new ModelAndView("login");
mav.addObject("usermodel",new UserModel());
return mav;
}


// problem part
public ModelAndView authenticateUser(HttpServletRequest request,
HttpServletResponse response,
Object command){
System.out.println(" iam in authentication"+command.getClass());
UserModel loginCommand = (UserModel) command;
// in user map we set the username as key
if( loginCommand.getUserName()!=null &&
loginCommand.getPassword()!=null &&
(loginCommand.getPassword()).equals(
(this.getUserMap()).get(loginCommand.getUserName() ))
) {
System.out.println("you are valid user");
return new ModelAndView("Expense");
}
else {
System.out.println("you are NOT valid user");
//throw new UserNotFoundException();
return new ModelAndView("login");
}
}

public void setUserMap(Map userMap) {
this.userMap =userMap;
}

public Map getUserMap() {
return this.userMap;
}
}

MY JSP PAGE IS

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="spring" uri="/spring" %>


<html>
<head>
<title>
Login to Expense Management System
</title>
</head>
<body>

<table width="742" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2" style="color:#FFFFFF;background-color:#666699;height:100px;"><span style="font-size:24pt;">
EXPENSE MANAGEMENT SYSTEM
</td>
</tr>
<tr>
<td width="742" valign="top">
<table width="742" border="0" cellspacing="0" cellpadding="0">
<tr>
<td
style="font-size:14pt;color:#FFFFFF;background-color:#9999CC;" align="right">
Login
</td>
</tr>
<tr>
<td valign="top" style="font-size:10pt;" align="center">
<form method="POST"
action="auth.htm">
<table width="400"
border="0"
cellspacing="5"
cellpadding="0">
<tr>
<td align="right" valign="middle">
<b>Login:</b>
</td>
<td align="left" valign="middle">
<spring:bind path="usermodel.userName">
<input type="text"
name="<cut value="${status.expression}"/>" value="<cut value="${status.value}"/>">
</spring:bind>
</td>
</tr>
<tr>
<td align="right" valign="middle">
<b>Password:</b>
</td>
<td align="left" valign="middle">
<spring:bind path="usermodel.password">

<input type="password"
name="<cut value="${status.expression}"/>" value="<cut value="${status.value}"/>">

</spring:bind>
</td>
</tr>
<td colspan="2" align="center">
<input type="submit">
</td>
</table>
</form>
<br>
<br>
<a href=""><B>FORGOT PASSWORD</B></a>.
<br><br> 
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

AND XML FILE IS

<beans>

<bean id="simpleUrlMapping"
class = "org.springframework.web.servlet.handler.SimpleUrl HandlerMapping">
<property name ="mappings">
<props>
<prop key ="login.htm">loginController</prop>
<prop key ="auth.htm">loginController</prop>
</props>

</property>
</bean>




<!-- ========================= CONTROLLER DEFINITIONS ========================= -->
<bean id="loginController"
class ="com.dss.ems.controllers.LoginController">
<property name="methodNameResolver" ref="LoginControllerMethodNameResolver"/>
<property name="userMap">
<map>
<entry>
<key><value>gp</value></key>
<value>gp</value>
</entry>
<entry>
<key><value>jk</value></key>
<value>jk</value>
</entry>
<entry>
<key><value>sb</value></key>
<value>sb</value>
</entry>
<entry>
<key><value>dd</value></key>
<value>dd</value>
</entry>
</map>
</property>

</bean>
<!-- =========================Method name resolver ========================= -->

<bean id="LoginControllerMethodNameResolver"
class="org.springframework.web.servlet.mvc.multiac tion.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/login.htm">showLoginPage</prop>
<prop key="/auth.htm">authenticateUser</prop> </props>

</property>
</bean>

<!--
<bean id="loginFormValidator"
class="com.dss.ems.formvalidators.LoginFormValidat or"/>
-->




<bean id="viewResolver"
class = "org.springframework.web.servlet.view.InternalReso urceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlVi ew</value>
</property>
<property name ="prefix">
<value>/jsp/</value>
</property>
<property name ="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>

AND ERROR ON SERVER SIDE IS


INFO: Using JSP 2.0 ExpressionEvaluator
iam in authentication class java.lang.Object
14-Dec-2005 14:27:16 org.springframework.web.servlet.FrameworkServlet processRequest
SEVERE: Could not complete request
java.lang.ClassCastException
at com.dss.ems.controllers.LoginController.authentica teUser(LoginController.java:
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.j
at java.lang.reflect.Method.invoke(Method.java:324)
at org.springframework.web.servlet.mvc.multiaction.Mu ltiActionController.invokeNa
Controller.java:403)
at org.springframework.web.servlet.mvc.multiaction.Mu ltiActionController.handleRe
tionController.java:358)
at org.springframework.web.servlet.mvc.AbstractContro ller.handleRequest(AbstractC
at org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter.handle(Simp
apter.java:44)
at org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet


problem is that how should i tell my MultiActionController Command Object is Of type com.dss.ems.models.Usermodel and not java.lang.Object



please help me friends

[ December 12, 2005: Message edited by: ganesh pol ]
[ December 14, 2005: Message edited by: ganesh pol ]
Rajarshi Ghosh
Greenhorn

Joined: Aug 31, 2005
Posts: 1
Hi,
I did it in this way:


I did not set anything in the xml file, but the form fields mataches to the CalVO.
Hope this helps you.
Arun kumar
Greenhorn

Joined: Apr 22, 2008
Posts: 10
In the old method definition
void ModelAndView xxxxx(HttpServletRequest req , HttpServletResponse res , Object obj ){}

change the Object class to the Class that you want to convert
ex
void ModelAndView xxxxx(HttpServletRequest req , HttpServletResponse res , User obj ){}

So that while binding the Class of the User command object will be taken and the values from the request parameters will be bound


Regards,<br /> Arun Kumar<br />----------------------------------<br />SCJP 5.0 86%
Reshma Beerantbail
Greenhorn

Joined: Jul 09, 2008
Posts: 1
Rajarshi Ghosh and Arunkumar spk,
Could you please elaborate on your answers? Thanks!

I'm new to Spring as well (and first time poster) and have the exact same error. The bind method mentioned above - where did you add it and how did you use it?

Thanks for any info!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

"Arunkumar spk" and "Reshma RB", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
sudheshna Iyer
Ranch Hand

Joined: Aug 20, 2004
Posts: 71
Team,

I have the same problem with the bind. How do I bind the command object in controller.

Did anybody solve this problem. If yes, can you please post the code for:
1. accessing command object in method of MultiactionController
2. JSP code ( do I need any additional tags to bing object)?
Yusuf
Greenhorn

Joined: Dec 16, 2008
Posts: 1
Hi I also have the same question.
Flow from JSP-->Controller-->Jsp in multiactionController is vague to me.

My MultiActionController:-
public ModelAndView changePassword(HttpServletRequest request, HttpServletResponse response, Users users) {
/*My Code here.
*/
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("changepassword");
modelAndView.addObject("users", users);
return modelAndView;

}


my Jsp:
<form action="changepassword.htm">
<spring:bind path="users.password">
<input type="password" name="password">
</spring:bind>
</form>

I get this exception & nothing is displayed on the browser window:-
javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'users' available as request attribute


Yusuf<br />SCJP1.5 - 93%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to tell MultiActionController Command Object is Of type com.dss.ems.model.User
 
Similar Threads
Spring validation
Neither BindingResult nor plain target object for bean name 'vids2m1' available as request attribute
ClassCastException in struts - urgent! :(
validation
Unable to Redirect to the On submitting a form