Below is my code, and always get null for query method, i do not know why this happen. I can get the form param by other ways, such as #RequestParam etc. Do i need to set configuration for the Phone Class in the XML file? if so, how can i configure Phone Class in XML? My XML is recharge-servlet.xml.
Below is the Controller:
package com.nu.template.recharge;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
#Controller
#RequestMapping("/mobile")
public class XJMobile {
#RequestMapping(value = "/index", method = RequestMethod.GET)
public ModelAndView getIndexPage() {
ModelAndView mav = new ModelAndView("recharge");
return mav;
}
#RequestMapping(value = "/query", method = RequestMethod.POST)
public String query(Phone phone) {
System.out.println(phone.getNumber());
return "helloworld";
}
}
Form:
<form action="query" method="POST" name="phone_form">
<section>
<label>phone:</label>
<input type="text" name="phone_number" />
</section>
<input type="submit" value="submit">
</form>
Phone Class:
package com.nu.template.recharge;
public class Phone {
private String phone_number;
public String getNumber() {
return this.phone_number;
}
public void setNumber(String phone_number) {
this.phone_number = phone_number;
}
}
recharge-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.nu.template.recharge"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
You can get your Phone object initialized by
#RequestMapping(value = "query", method = RequestMethod.POST)
public String query(#RequestParam("phone_number") Phone phone) {
System.out.println(phone.getNumber());
return "helloworld";
}
Spring generally looks for same parameter type for binding or you can tell it to bind with your pojo, given a setter method which accepts parameter type.
Related
<context:annotation-config />
when above line is added in ApplicationContext.xml file the Test.java is not running but when i add this it works but in the tutorial i was going through it says both the methods work can someone help whats the problem with
above line of code.
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
Employee.java
package com.stp.beans;
import org.springframework.beans.factory.annotation.Autowired;
public class Employee {
private Address empaddress;
public Address getEmpaddress() {
return empaddress;
}
#Autowired
public void setEmpaddress(Address empaddress) {
this.empaddress = empaddress;
}
}
Address.java
package com.stp.beans;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id ="empbean" class="com.stp.beans.Employee"></bean>
<bean id ="adrbean" class="com.stp.beans.Address"></bean>
</beans>
Test.java
package com.stp.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.stp.beans.Employee;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("/com/stp/cfgs/ApplicationContext.xml");
Employee emp = (Employee) context.getBean("empbean");
System.out.println(emp.getEmpaddress());
}
}
When you use <context:annotation-config />, your ApplicationContext.xml should be like below.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:annotation-config />
<bean id ="empbean" class="com.stp.beans.Employee"></bean>
<bean id ="adrbean" class="com.stp.beans.Address"></bean>
</beans>
You had just missed the context related definitions.
I'm using Hibernate validator with Hibernate and Spring, but it seems that validations don't work. When I don't enter a String or enter a String of 1 character (which is not between min=4 and max=20) is not showing any error and therefore saves in the table. What am I missing?
package dao;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
#Entity
#Table(name="etudiant")
public class Etudiant {
#Id
#Column(name="ID_ETUDIANT")
#GeneratedValue
private Long idEtudiant;
#Column(name="NOM")
#Size (min=4, max=20)
private String nom;
#Size(min=4, max=20, message="nom doit etre entre 4 et 20 svp..")
#Column(name="PRENOM")
private String prenom;
#Column(name="DATE_NAISSANCE")
#Temporal(TemporalType.DATE)
private Date dateNaissance;
#NotNull
#Column(name="EMAIL")
private String email;
public Etudiant() {
super();
}
public Long getIdEtudiant() {
return idEtudiant;
}
public void setIdEtudiant(Long idEtudiant) {
this.idEtudiant = idEtudiant;
}
public Etudiant(String nom, String prenom, Date dateNaissance, String email) {
super();
this.nom = nom;
this.prenom = prenom;
this.dateNaissance = dateNaissance;
this.email = email;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public Date getDateNaissance() {
return dateNaissance;
}
public void setDateNaissance(Date dateNaissance) {
this.dateNaissance = dateNaissance;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Controller:
#RequestMapping(value="save",method=RequestMethod.POST)
public String saveetudient(Model md,
#Valid #ModelAttribute("etudiant") Etudiant et,
BindingResult res) {
if (res.hasErrors()) {
List <Etudiant> ets=service.listeEtudiants();
md.addAttribute("etudiants",ets);
return "etudiant1";
}
else {
service.addEtudiant(et);
List <Etudiant> ets=service.listeEtudiants();
md.addAttribute("etudiants",ets);
return "etudiant1";
}
}
In JSP I put this line to show errors:
<form action="save" method="post">
<table border="1" width="500" bgcolor="grey">
<tr>
<th>Nom </th>
<th>Prenom </th>
<th> Date de naissance</th>
<th>Email </th>
</tr>
<tr>
<td> <input type="text" name="nom" > </td>
<td> <input type="text" name="prenom" > </td>
<td> <input type="text" name="dateNaissance" > </td>
<td> <input type="text" name="email" > </td>
</tr>
</table> <br>
<input type="submit" value="ajouter">
<sform:errors path="etudiant.*"/>
<sform:errors path="prenom"/>
</form>
XML file configuration :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
<context:component-scan base-package="controller"/>
<mvc:annotation-driven/>
<bean class="dao.DaoEtudiantImpl" name="daoetud"></bean>
<bean class="service.EtudiantMetierImpl" name="servetud">
<property name="dao" ref="daoetud"></property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
Finally I found the error ,
the problem was that I didin't add hibernate-validator jar I only added files in the dist/lib/required :
classmate-1.3.1
javax.el-2.2.4
-javax.el-api-2.2.2
jboss-logging-3.3.0.Final
validation-api-1.1.0
So if that happens to someone just add : hibernate-validator-5.3.2.Final.
After spent one hour to study two validation tutorials:
Hibernate Validator Annotations Example
Spring MVC Form Validation Annotation Example
I suggest you some solutions.
1) Verify hibernate validation annotation working or not
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Etudiant>> constraintViolations = validator.validate(etudiant);
2) You can create custom validator, for example EtudiantValidator, then validate manually like below:
#RequestMapping(value="save",method=RequestMethod.POST)
public String saveetudient(Model md,#ModelAttribute("etudiant") Etudiant etudiant, BindingResult res){
new EtudiantValidator().validate(etudiant, result);
...
}
However I deep into your code,I see that you have a mistake on this method:
#RequestMapping(value="save",method=RequestMethod.POST)
public String saveetudient(Model md,
#Valid #ModelAttribute("etudiant") Etudiant et,
BindingResult res)
Should change it to like below
#RequestMapping(value="save",method=RequestMethod.POST)
public String saveetudient(Model md,
#Valid #ModelAttribute("etudiant") Etudiant etudiant,
BindingResult res)
Hope this help!
I want to check size validation for studentHobby object and i am using #Size annotation and Using #valid annotation.
I am providing less than the value defined in #Size annotation but still I am getting the result instead of error.
I tried things but didn't come with solutions.
StudentAdmissionController.java
package com.diwakar;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class StudentAdmissionController {
#InitBinder
public void initBinder(WebDataBinder binder) {
//binder.setDisallowedFields(new String[] {"studentMobile"});
//SimpleDateFormat date = new SimpleDateFormat("dd**MM**yyyy");
//binder.registerCustomEditor(Date.class, "studentDOB", new CustomDateEditor(date, false));
binder.registerCustomEditor(String.class, "studentName", new StudentNameEditor());
}
#RequestMapping(value="/admission.html", method = RequestMethod.GET)
public ModelAndView getAdmissionForm() {
ModelAndView model = new ModelAndView("AdmissionForm");
//model.addObject("headerMessage", "Diwakar College of Engineering.!!");
return model;
}
#RequestMapping(value="/submitForm", method = RequestMethod.POST)
public ModelAndView submitAdmissionForm(#Valid #ModelAttribute("st1") Student st1, BindingResult result) {
if (result.hasErrors()) {
ModelAndView model = new ModelAndView("AdmissionForm");
return model;
}
ModelAndView model = new ModelAndView("AdmissionSuccess");
//model.addObject("headerMessage", "Diwakar College of Engineering.!!");
return model;
}
#ModelAttribute
public void addCommonMessage(Model model) {
model.addAttribute("headerMessage", "Diwakar College of Engineering.!!");
}
Student.java
package com.diwakar;
import java.util.ArrayList;
import java.util.Date;
import javax.validation.constraints.Size;
public class Student {
private String studentName;
#Size(min=3, max=10)
private String studentHobby;
private Long studentMobile;
private Date studentDOB;
private ArrayList<String> studentSkills;
private Address studentAddress;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentHobby() {
return studentHobby;
}
public void setStudentHobby(String studentHobby) {
this.studentHobby = studentHobby;
}
public Long getStudentMobile() {
return studentMobile;
}
public void setStudentMobile(Long studentMobile) {
this.studentMobile = studentMobile;
}
public Date getStudentDOB() {
return studentDOB;
}
public void setStudentDOB(Date studentDOB) {
this.studentDOB = studentDOB;
}
public ArrayList<String> getStudentSkills() {
return studentSkills;
}
public void setStudentSkills(ArrayList<String> studentSkills) {
this.studentSkills = studentSkills;
}
public Address getStudentAddress() {
return studentAddress;
}
public void setStudentAddress(Address studentAddress) {
this.studentAddress = studentAddress;
}
}
spring-dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- <bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean name="/welcome.html" class="com.diwakar.HelloController" /> -->
<context:component-scan base-package="com.diwakar" />
<mvc:annotation-driven />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
Here is my admission page even though i am providing one one character it is giving me submit form instead of error.
admission.html Page
This submit form i am getting after submitting value with 1 character.
submitForm
Can you please help me figure out how to invoke the login page by using my controller?
Here is my code:
package com.mvc.demo;
public class Emp {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
MvcDemo.java (it's my controller; just for invoking login page)
package com.mvc.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
public class MvcDemo {
#RequestMapping(value="/login", method = RequestMethod.GET)
public String showForm(Emp em) {
return "login";
}
}
dispatcher-servlet.xml
<context:component-scan base-package="com.mvc.demo" />
<mvc:annotation-driven />
<beans>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
web.xml
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
login.jsp
<form:form action="#" method = "post" modelAttribute="emp">
<form:label path="username">Enter your user-name</form:label>
<form:input id="username" name="username" path="name" /><br>
<form:label path="username">Please enter your password</form:label>
<form:password id="password" name="password" path="password" /><br>
<input type="submit" value="Submit" />
</form:form>
project Structure:
MvcDemo
JavaResources
src
com.mvc.demo
WebContent
jsp
login.jsp
WEB-INF
lib
web.xml
dispatcher-servlet.xml
index.jsp
You are missing #Controller annotation on your controller class. Spring does not create a handler for url unless you instantiate a controller by using annotation.
It does the same as in your code,when you hit url with localhost:8080/Mvc_Demo/login it must show your login.jsp,
hope this solves your problem.
package com.mvc.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
public class MvcDemo {
#RequestMapping(value="/login", method = RequestMethod.GET)
public String showForm( )
{
ModelAndView mv = new ModelAndView("login");
return mv;
}
}
Try this code:
package com.mvc.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
public class MvcDemo {
// To call the view for login
#RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login() {
return new ModelAndView("login","newEmp", new Emp());
}
// To call the validate login after submit
#RequestMapping(value = "/user-login", method = RequestMethod.POST)
#ResponseBody
public ModelAndView userLogin(Emp emp) {
//TODO check 'emp' object to validate user
return new ModelAndView("home");
}
}
I was reading all the posts on this issue, but none of them helped.
I am trying to persist my entity to MySQL using Hibernate, JPA on Spring Framework 3.0.1. I can find rows from database and display, to do that when the form displayed the textbox holds fields fron the database, but when I try to save to the database, it doesn't change any row at all.
My configuration looks like below ...
My persistence.xml ...
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="userpersistence" transaction-type="RESOURCE_LOCAL">
<provider>
org.hibernate.ejb.HibernatePersistence
</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/acctdatabase"/>
<property name="hibernate.connection.username" value="acct-user"/>
<property name="hibernate.connection.password" value="password"/>
</properties>
</persistence-unit>
</persistence>
My Entity:
package edu.acct.tsegay.model;
import javax.enterprise.context.RequestScoped;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;
import org.springframework.beans.factory.annotation.Autowired;
#Entity
#Table(name = "programs")
public class Program {
private int prog_Id;
private String prog_Name;
public Program() {
super();
}
// #GeneratedValue
#Id
public int getProg_Id() {
return prog_Id;
}
public void setProg_Id(int progId) {
prog_Id = progId;
}
public String getProg_Name() {
return prog_Name;
}
public void setProg_Name(String progName) {
prog_Name = progName;
}
public String toString() {
return "This is a String";
}
}
My DAO:
package edu.acct.tsegay.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import edu.acct.tsegay.model.Program;
#Repository
public class ProgramDaoImpl implements IProgramDao {
protected EntityManager entityManager;
#PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
#SuppressWarnings("unchecked")
public List<Program> getProgram(int progId) {
// TODO Auto-generated method stub
return entityManager.createQuery("from program o").getResultList();
}
public void save(Program program) {
entityManager.merge(program);
}
public Program getProg(int id) {
return entityManager.createQuery(
"SELECT c FROM Program c WHERE c.prog_Id = :id", Program.class)
.setParameter("id", id).getSingleResult();
}
}
My Service:
package edu.acct.tsegay.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import edu.acct.tsegay.dao.IProgramDao;
import edu.acct.tsegay.model.Program;
#Service("programSerivce")
public class ProgramServiceImpl implements IProgramService {
#Autowired
private IProgramDao programDao;
public IProgramDao getProgramDao() {
return programDao;
}
public void setProgramDao(IProgramDao programDao) {
this.programDao = programDao;
}
#Transactional(propagation = Propagation.REQUIRES_NEW)
public void save(Program program) {
programDao.save(program);
}
public Program getProgram(int id) {
return programDao.getProg(id);
}
}
My contextApplication
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="org.h2.Driver"
p:url="jdbc:h2:mem:gallery;DB_CLOSE_DELAY=-1" p:username="sa"
p:password="" />
<bean id="mysqlDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.databaseurl}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="mysqlDataSource" />
<!-- MYSQL SERVER 2008 database connection -->
<bean id="entityManagerFactoryMSSQL"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="mssqlDataSource" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory" />
<bean id="userDao" class="edu.acct.tsegay.dao.UserDaoJpa" />
<bean id="programDao" class="edu.acct.tsegay.dao.ProgramDaoImpl" />
<bean id="userSerivce" class="edu.acct.tsegay.service.UserServiceImpl" />
<bean id="programSerivce" class="edu.acct.tsegay.service.ProgramServiceImpl" />
<tx:annotation-driven mode="aspectj"
transaction-manager="transactionManager" />
</beans>
My Controller:
package edu.acct.tsegay.controller;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import edu.acct.tsegay.model.Program;
import edu.acct.tsegay.model.User;
import edu.acct.tsegay.service.IProgramService;
import edu.acct.tsegay.service.IUserService;
#Controller
public class HelloController {
#Autowired
#Qualifier("userSerivce")
private IUserService<User> userService;
#Autowired
#Qualifier("programSerivce")
private IProgramService programService;
public void setUserService(IUserService<User> userService) {
this.userService = userService;
}
public void setProgramService(IProgramService programService) {
this.programService = programService;
}
#RequestMapping(value = "/form", method = RequestMethod.GET)
public ModelAndView displayForm(ModelAndView model) {
Program program = new Program();
program.setProg_Name(programService.getProgram(16).getProg_Name());
model.addObject("program", program);
return model;
}
// Process the form.
#SuppressWarnings("unchecked")
#RequestMapping(value = "/form", method = RequestMethod.POST)
public String processForm(Program program, Map model) {
Program prog = new Program();
prog.setProg_Name("New progrma");
programService.save(prog);
Program pro = programService.getProgram(17);
// Add the saved book to the model
model.put("pro", pro);
model.put("program", program);
return "program";
}
#RequestMapping("/bye")
public ModelAndView sayBye() {
ModelAndView model = new ModelAndView("bye");
model.addObject("msg", "This is a Message from Tsegay done ..");
return model;
}
#RequestMapping("/footer")
public ModelAndView displayFooter() {
ModelAndView model = new ModelAndView("footer");
model.addObject("msg", "This is a footer ..");
return model;
}
}
My view
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!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=ISO-8859-1">
<title>Tsegay | Form</title>
</head>
<body>
<form:form action="form.tsegay" method="POST" commandName="program">
<table>
<tr>
<td>First Name:</td>
<td><form:input path="Prog_Id" /> <form:errors path="Prog_Id" />
</td>
</tr>
<tr>
<td>Password:</td>
<td><form:input path="Prog_Name" /> <form:errors
path="Prog_Name" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Login" /></td>
</tr>
</table>
</form:form>
</body>
</html>
The forum post Transactions are not starting saved my life. You need to update (see the question):
<tx:annotation-driven mode="proxy"
transaction-manager="transactionManager" />