Spring and Struts2 complete example

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" xmlns ="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...ring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="customerAction" class="com.san.action.CustomerAction" > </bean> <bean id="addCustomer" class="com.san.action.RegistrationAction" > </bean> <context:component-scan base-package="com.san" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource"> <property name="driverClassName" value="org.hsqldb.jdbcDriver"/> <property name="url" value="jdbc:hsqldb:file:./cred;shutdown=true"/> <property name="username" value="SA"/> <property name="password" value=""/> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotati on.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.HSQL Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.cglib.use_reflection_optimizer">tru e</prop> <!--<prop key="hibernate.cache.provider_class">net.sf.hibern ate.cache.HashtableCacheProvider</prop> --> </props>

web.Stru tsPrepareAndExecuteFilter</filterclass> </filter> <listener> <listener-class>org.5"> <display-name>Struts2Example14</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.filter.org/dtds/struts-2.orm.dispatcher.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.apache.xml <?xml version="1.context.springframework.struts2.com/xml/ns/javaee" xmlns:web="http://java.Hibernat eTransactionManager" p:sessionFactory-ref="sessionFactory" /> </beans> Struts.san.sun.jsp</result> </action> </package> </struts> Web.com/xml/ns/javaee http://java.xsd" xsi:schemaLocation="http://java.apache.sun.sun.springframework.xml (inside web-inf/classes) <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.CreditCard</value> </list> </property> </bean> <bean id="transactionManager" class="org.</property> <property name="annotatedClasses"> <list> <value>com.dtd"> <struts> <package name="default" extends="struts-default"> <action name="addCustomer" class="customerAction"> <result name="SUCCESS">/addCustomer.xsd" id="WebApp_ID" version="2.jsp</result> </action> <action name="customerAction" class="customerAction"> <result name="SUCCESS">/success.sun.com/xml/ns/javaee/webapp_2_5.ng.w3.0//EN" "http://struts.hibernate3.ContextLoade rListener</listener-class> </listener> .0.com/xml/ns/javaee/web-app_2_5.org/2001/XMLSchema-instance" xmlns="http://java.

jsp <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h2>Add Customer</h2> <s:form action="customerAction" > <s:textfield name="customerBean.<filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> addCustomer.URL=addCustomer.age" /><br> </body> </html> .age" label="Age" value=""/> <s:submit /> </s:form> </body> </html> Index.jsp <META HTTP-EQUIV="Refresh" CONTENT="0.jsp <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 ModelDriven example</h1> <h2>Customer Details</h2> Name : <s roperty value="customerBean.name" /><br> Age : <s roperty value="customerBean.action"> Success.name" label="Name" /> <s:textfield name="customerBean.

Logger. // Retrieve session from Hibernate Session session = sessionFactory.getCurrentSession(). // Create a Hibernate query (HQL) Query query = session. import org.springframework.log4j.SessionFactory. import java. import org.ArrayList. @Resource(name="sessionFactory") private SessionFactory sessionFactory. } /** * Retrieves a single credit card .debug("Retrieving all credit cards").list(). /** * Service for processing Credit Cards * */ @Service("creditCardService") @Transactional public class CreditCardService { protected static Logger logger = Logger. import javax.util. /** * Retrieves all credit cards */ public List<CreditCard> getAll() { logger.Session.san.transaction. import java.getLogger("CreditCardService. import org.class").springframework. // Retrieve all return query. import org.List.Transac tional.annotation.package com.createQuery("FROM CreditCard").hibernate.Resource.annotation. import org.hibernate.Service.stereotype. import org.apache.util.Query.hibernate.

createSQLQuery("DELETE FROM PERSON_CREDIT_CARD " + "WHERE creditCards_ID="+id).class.class.delete(creditCard). . id). query. // Persists to db return creditCard.get(CreditCard.*/ public CreditCard get( Integer id ) { // Retrieve session from Hibernate Session session = sessionFactory. // Delete reference to foreign key credit card first // We need a SQL query instead of HQL query here to access the third table Query query = session.get(CreditCard.getCurrentSession().class. // Retrieve existing credit card via id // CreditCard existingCreditCard = (CreditCard) session.executeUpdate().getCurrentSession().debug("Deleting existing credit card").getCurrentSession().get(CreditCard. id). } /** * Edits an existing credit card */ public void edit(CreditCard creditCard) { logger. // Delete session. } /** * Deletes an existing credit card */ public void delete(Integer id) { logger. // Retrieve session from Hibernate Session session = sessionFactory. // Retrieve existing credit card CreditCard creditCard = (CreditCard) session.debug("Editing existing creditCard"). // Retrieve existing credit card CreditCard creditCard = (CreditCard) session. // Retrieve session from Hibernate Session session = sessionFactory.

Column. //existingCreditCard.executeUpdate().getNumber( )).creditCard.persistence.Table.getCurrentSession(). /** * Represents a credit card entity * **/ @Entity @Table(name = "CREDIT_CARD") public class CreditCard implements Serializable { private static final long serialVersionUID = 5924361831551833717L. @Id //@GeneratedValue . } public void createTable() { logger.persistence.createSQLQuery("CREATE TABLE CREDIT_CARD (ID INTEGER. TYPE VARCHAR(20). query.getId()). import java. NUMBER VARCHAR(20))").Id. import javax. import javax. import javax.persistence. // Delete reference to foreign key credit card first // We need a SQL query instead of HQL query here to access the third table Query query = session. import javax.san.save(creditCard).getType()).setType(creditCard. // Retrieve session from Hibernate Session session = sessionFactory.persistence.Serializable.persistence.io.setNumber(creditCard.Entity.GeneratedValue. // Assign updated values to this credit card //existingCreditCard.debug("Deleting existing credit card"). // Save updates session. import javax. } } package com.

import java.san.CreditCardService.util. @Column(name = "TYPE") private String type.ArrayList. } public String getType() { return type.util.beans.factory. import org.san. import java. } public void setType(String type) { this.number = number.action. import com.Date.san. } public void setNumber(String number) { this.id = id. .Autow ired.type = type. public Integer getId() { return id.util. } } package com. import com. } public void setId(Integer id) { this.annotation.List. @Column(name = "NUMBER") private String number.CreditCard. } public String getNumber() { return number.@Column(name = "ID") private Integer id. import java.springframework.

getName()). } Customer customerBean. } public void setCustomerBean(Customer customerBean) { this.getAge()).println(customerBean.customerBean = customerBean. // System.edit(cc). cc. cc.public class CustomerAction { @Autowired CreditCardService ccservice. public Customer getCustomerBean() { return customerBean.setId(15).setType(customerBean. public String execute() { CreditCard cc = new CreditCard(). return "SUCCESS". cc.out.setNumber(customerBean.getName()). } } . ccservice.

Sign up to vote on this title
UsefulNot useful