Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Form values not setting in Spring hibernate annotation

 
shikhaj jainy
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I am new to spring hibernate annotation.Earlier I have done some work on spring but not too much experienced .
I am trying to save data in db but form values are not setting up. I am giving some code snippet ,any help will be appreciated.

Controller class
//start of controller code

@Controller
@SessionAttributes("employee")
public class EmployeeController {

@Autowired(required = false)
private EmployeeService employeeService;

//This method will give empty form in case of Add employee and in Update opertaion it will fetch value from db (to be updated)
@RequestMapping(value="/employeeForm.html",method = RequestMethod.GET)
public Employee setUpForm(@RequestParam(value = "name", required = false) String employeeName) {
if (employeeName == null) {
return new Employee();
} else {
return employeeService.getEmployeeByName(employeeName);
}

}


@RequestMapping(value="/saveEmployee", method=RequestMethod.GET)
public String save(Employee employee){

//no form data is being set here

employeeService.saveEmployee(employee);
return "save";
}


//end of controller code

----------------------------------------------------------------------------------------------------------
Entity class
//Start of entity class code

@Entity
@Table(name = "RESOURCE_DATA")
public class Employee implements Serializable {

@Id
@GeneratedValue
@Column(name = "ID")
private Integer id;

@Column(name = "E_NUMBER")
private Integer eNumber;
@Column(name = "ENAME")
private String name;
@Temporal(TemporalType.DATE)
@Column(name = "DOJ")
private Date dateOfJoining;
@Column(name = "ROLE")
private String role;
@Column(name = "TEAM")
private String team;


public Employee() {
}


public Employee (Integer id,Integer eNumber,String name,Date dateOfJoining,String role,String team) {
setId(id);
seteNumber(eNumber);
setName(name);
setDateOfJoining(dateOfJoining);
setRole(role);
setTeam(team);

}

//setter n getters
}

//end of entity class code

-------------------------------------


JSP code

//Start of JSP code

<form:form modelAttribute="employee" action="employeeForm.html" method="post">

<!-- form fields label n text-->

<input type="submit" value="save" name="save" onclick="window.location='saveEmployee.html'"/>

</form:form>

//End of JSP code

---------------


Note-I have included only required code for jsp because its too long and also not including xml files.If any of the xml required ,please let me know.
---------------

dispatcher-servlet.xml(for controllers)
<code>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<context:annotation-config/>

<!-- Auto-detect controllers in this package -->
<context:component-scan base-package="com.fis.coe.controller" />

<mvc:annotation-driven></mvc:annotation-driven>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>


<!-- Prepend /WEB-INF/jsp/ and append .jsp to the view name -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>


<!-- Access resource bundles with the specified basename -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="/WEB-INF/messages"/>


</beans>


</code>

------------------------------------

spring-database.xml(that scans dao and services)

<code>

<beans xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!--<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>database.properties</value>
</property>
</bean>-->

<!-- <context:property-placeholder location="WEB-INF/jdbc.properties"/>-->
<context:component-scan base-package="com.fis.coe.dao"/>
<context:component-scan base-package="com.fis.coe.service"/>

<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory">
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.fis.coe.model.Employee</value>
</list>
</property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="url"
p:username="root"
p:password="******t">
</bean>




</beans>

</code>

--------------------------------

Note-I am not using spring validation ,using javascript one.
I am sure ,I am missing something in entity or controller that is too basic.

Waiting for reply.


Thank You.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@RequestMapping(value="/saveEmployee", method=RequestMethod.GET)
public String save(Employee employee){

//no form data is being set here

Because a form submit is a POST, not a GET.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic