• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring:bind tag for multiple objects

 
snigdha
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

iam using spring framework and wanted to do registration form, which should relate the two tables through hibernate. While binding the bean objects with <Spring:bind> tag i registered the bean class with command class and gave some command name.

like <spring:bind path="userreg.personAddress">
where userreg is the command name given to the registration class.Similarly did for all other fields with same command name.But if i want to bind username and password to another beanobject i.e another table,i cannot add new command name to a new command class as below.

<property name="commandName">
<value>userreg</value>
</property>
<property name="commandClass">
<value>com.Person</value>
</property>
<property name="commandName">
<value>userlogin</value>
</property>
<property name="commandClass">
<value>com.user</value>
</property>

how can i bind different objects to the same command object
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"VDS VDS", please check your private messages.
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"snigdha",

You clearly haven't read the naming policy properly, because your name is still not ok.
 
pragas siva
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Test Post
 
pragas siva
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pragas siva wrote:Hi.

Just create one bean(Model). Inside bean create method to get second bean(Model).
using this method you can bind the second bean properties, in view.


Here is sample code for reference.

import java.util.Date;

/**
*
* @author pragaspathi.s
*/
public class TestBean1 {

private Date a;
private String b;
private String c;
private final TestBean2 tstbean2 = new TestBean2();

public TestBean2 getTstbean2() {

return tstbean2;
}

public Date getA() {
return a;
}

public void setA(Date a) {
this.a = a;
}

public String getB() {
return b;
}

public void setB(String b) {
this.b = b;
}

public String getC() {
return c;
}

public void setC(String c) {
this.c = c;
}
}
-----
TestBean2.java


public class TestBean2 {

private String a2;
private String b2;
private String c2;

public String getA2() {
return a2;
}

public void setA2(String a2) {
this.a2 = a2;
}

public String getB2() {
return b2;
}

public void setB2(String b2) {
this.b2 = b2;
}

public String getC2() {
return c2;
}

public void setC2(String c2) {
this.c2 = c2;
}



}

--------------
Testcontroller.java


package com.comobj.controllers;

import com.comobj.models.TestBean1;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.DataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

/**
*
* @author pragaspathi.s
*/
public class TestController extends MultiActionController {

public ModelAndView showForm(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mv = new ModelAndView("Comobj");
mv.addObject("testBean1", new TestBean1());
System.out.println("thisis called ");
return mv;
}

public ModelAndView submitValue(HttpServletRequest request, HttpServletResponse response, TestBean1 m) {
System.out.println("this is called form test controller");
TestBean1 min = m;
System.out.println("--> from tst a " + min.getA());
System.out.println("--> from tst b " + min.getB());
System.out.println("--> from tstbn2 a2 " + min.getTstbean2().getA2());
DataBinder bnd = new DataBinder(new TestBean1());
ModelAndView mv = new ModelAndView("index");
return mv;
}
}


-------------------------------------------
view

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

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Welcome to Spring Web MVC project</title>
</head>

<body>

<form:form action="submitValue.htm" method="POST" commandName="testBean1">
(TestBean1 property-->a) DATE : <spring:bind path="a">
<input type="input" name="a" id="stdate" value="<fmt:formatDate value="${a}" type="date" pattern="yyyy-MM-dd"/>"/>
</spring:bind>format(yyyy/mm/dd)
<br/>
(TestBean1 property-->b) <form:input path="b" />
<br/>
(TestBean2 property-->a2) <form:input path="tstbean2.a2" />
<input type="submit" value="check">
</form:form>
</body>
</html>

I hope this might be help full.
 
pragas siva
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic