Announcement Announcement Module
Collapse
No announcement yet.
Got prob when integrate Struts+XFire+Spring+JPA Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Got prob when integrate Struts+XFire+Spring+JPA

    Hi there,

    I've tested integrate Struts 1.3 + XFire 1.26 + Spring 2.5 + JPA followed tutorials and examples that I've found by googling.

    Finally, almost done but there are still some probs needed help.

    Here is my config:

    web.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app
    	xmlns="http://java.sun.com/xml/ns/javaee"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    		http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    	>
    
        <!-- contexts -->
        <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>/WEB-INF/log4j.properties</param-value>
        </context-param>
        
        <context-param>
       	<param-name>contextConfigLocation</param-name>
            <param-value>
       		/WEB-INF/applicationContext.xml
       	</param-value>
        </context-param> 
      	 	
        <!-- listerners -->    
        <listener>
            <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
        </listener>
      	
        <listener>
        	<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
        </listener>
      	
        <listener>
    	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    		
        <!-- servlets -->
        <servlet>
        	<servlet-name>xfire</servlet-name>
        	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        	<load-on-startup>1</load-on-startup>
        </servlet> 
    
        <servlet>
        	<servlet-name>action</servlet-name>
        	<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        	<init-param>
          	     <param-name>config</param-name>
          		<param-value>/WEB-INF/struts-config.xml</param-value>
        	</init-param>
        	<init-param>
          		<param-name>debug</param-name>
          		<param-value>3</param-value>
        	</init-param>
        	<init-param>
          		<param-name>detail</param-name>
          		<param-value>3</param-value>
        	</init-param>
        	<load-on-startup>2</load-on-startup>
        </servlet>
      	
        <!-- mappings -->
        <servlet-mapping>
        	<servlet-name>action</servlet-name>
        	<url-pattern>*.do</url-pattern>
        </servlet-mapping>
    
        <servlet-mapping>
        	<servlet-name>xfire</servlet-name>
        	<url-pattern>/services/*</url-pattern>
        </servlet-mapping>
      	
        <!-- filter -->
        <filter>
            <filter-name>encodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
        </filter>
        
        <filter-mapping>
    	<filter-name>encodingFilter</filter-name>
    	<url-pattern>/*</url-pattern>
        </filter-mapping>	
      	
        <!-- welcome file list -->
        <welcome-file-list>
        	<welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
      	
    </web-app>
    applicationContext.xml
    Code:
    <?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:util="http://www.springframework.org/schema/util"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    		http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    		http://www.springframework.org/schema/util
    		http://www.springframework.org/schema/util/spring-util-2.0.xsd
    		http://www.springframework.org/schema/context
    		http://www.springframework.org/schema/context/spring-context-2.1.xsd
    		http://www.springframework.org/schema/tx
    		http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
    	>
    	
    	<!-- config JPA capabilites -->
    	<bean id="loadTimeWeaver" class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    	
    	<bean id="vendorAdapter" class="org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter">
    		<property name="databasePlatform" value="oracle.toplink.essentials.platform.database.oracle.OraclePlatform" />
    		<property name="showSql" value="false" />
    		<property name="generateDdl" value="false" />
    	</bean>
    	
    	<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    		<property name="persistenceXmlLocations">
    			<list>				
    				<value>classpath*:/META-INF/persistence.xml</value>
    			</list>
    		</property>
    		<property name="loadTimeWeaver" ref="loadTimeWeaver" />
    	</bean>
    	
    	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="persistenceUnitName" value="DTKBPU" />
    		<property name="jpaVendorAdapter" ref="vendorAdapter" />
    		<property name="persistenceUnitManager" ref="persistenceUnitManager" />
    	</bean>
    	
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    	</bean>
    	
    	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    	
    	<tx:annotation-driven transaction-manager="transactionManager" />
    			
    	<!-- DAOs -->	
    	<bean name="userDAO" class="vn.kbnn.dtkb.system.dtos.UserDAO">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    	</bean>
    		
    </beans>
    persistence.xml (place both src/META-INF and WEB-INF)
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <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_1_0.xsd" version="1.0">
        
    	<persistence-unit name="DTKBPU" transaction-type="JTA">
        	<provider>oracle.toplink.essentials.PersistenceProvider</provider>
        	<jta-data-source>jdbc/dtkb</jta-data-source>
        	<class>vn.kbnn.dtkb.system.dtos.User</class>
        	<properties>
        		<property name="toplink.logging.level" value="INFO" />
        	</properties>
    	</persistence-unit>  
    </persistence>
    struts-config.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
    
    <struts-config>
      	<form-beans />
      	<global-exceptions />
        <global-forwards />
      	<action-mappings type="vn.kbnn.framework.web.XActionMapping">
            <action path="/prelog" parameter="PRELOG"
                type="vn.kbnn.dtkb.system.web.actions.LoginAction">
                <set-property property="loginRequire" value="false"/>
                <forward name="cookied" redirect="false" path="/login.do"/>
                <forward name="notcookied" redirect="false" path="/login.jsp"/>
            </action>
            <action path="/logout" parameter="LOGOUT"
                type="vn.kbnn.dtkb.system.web.actions.LoginAction">
                <set-property property="loginRequire" value="false"/>
                <forward name="success" redirect="false" path="/login.jsp"/>
                <forward name="failure" redirect="false" path="/failures/failure.jsp"/>
            </action>
            <action path="/login" parameter="LOGIN"
                type="vn.kbnn.dtkb.system.web.actions.LoginAction">
                <set-property property="loginRequire" value="false"/>
                <forward name="success" redirect="false" path="/main.jsp"/>
                <forward name="error" redirect="false" path="/login.jsp"/>
                <forward name="failure" redirect="false" path="/failures/failure.jsp"/>
            </action>          	
      	</action-mappings>
      	<message-resources parameter="vn.kbnn.dtkb.web.ApplicationResources" />
    </struts-config>
    LoginAction extends DispatchActionSupport and have a call to method of UserService

    Code:
    	private ActionForward login(ActionMapping _actionMapping,
    								ActionForm _actionForm,
    								HttpServletRequest _httpServletRequest,
    								HttpServletResponse _httpServletResponse) {
    		Service serviceModel;
    		HttpSession httpSession;
    		httpSession = _httpServletRequest.getSession();
    		serviceModel = new AnnotationServiceFactory().create(UserServiceImpl.class);
    		try {
    			User paraUser = (User)getMaster("vn.kbnn.dtkb.system.dtos.User", _httpServletRequest);
    			try {
    				IUserService userService = (IUserService) new XFireProxyFactory().create(
    						serviceModel, "http://localhost/DTKB/services/UserService?wsdl");
    				User user = userService.login(paraUser);
    				httpSession.setAttribute(Constant.USER_SESSION_KEY, user);
    				if (user.getId() != "") {
    					return _actionMapping.findForward("success");
    				} else {
    					return _actionMapping.findForward("error");
    				}
    			} catch (MalformedURLException murle) {
    				_httpServletRequest.setAttribute(Constant.ERROR_MESSAGE_REQUEST_KEY, murle.getMessage());
    				return _actionMapping.findForward("failure");
    			}
    		} catch (Exception e) {
    			_httpServletRequest.setAttribute(Constant.ERROR_MESSAGE_REQUEST_KEY, e.getMessage());
    			return _actionMapping.findForward("failure");
    		}
    	}

  • #2
    User service implemented by an inteface IUserService and an implementation UserServiceImpl

    IUserService.java
    Code:
    package vn.kbnn.dtkb.business.services.system;
    
    import javax.jws.WebParam;
    import javax.jws.WebService;
    import javax.jws.soap.SOAPBinding;
    
    import vn.kbnn.dtkb.system.dtos.User;
    
    @WebService(name="UserService", targetNamespace ="http://kbnn.vn")
    @SOAPBinding(style=SOAPBinding.Style.DOCUMENT,
                 use=SOAPBinding.Use.LITERAL,
                 parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)
    public interface IUserService {
    	public User login(@WebParam User user);
    }
    UserServiceImpl.java
    Code:
    package vn.kbnn.dtkb.business.services.system;
    
    import javax.jws.WebService;
    
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    
    import vn.kbnn.dtkb.system.dtos.User;
    import vn.kbnn.dtkb.system.dtos.UserDAO;
    
    @WebService(serviceName = "services/UserService", endpointInterface = "vn.kbnn.dtkb.business.services.system.IUserService")
    public class UserServiceImpl implements ApplicationContextAware {
    	UserDAO userDAO;
    
    	private static ApplicationContext ctx;
    	
    	public User login(User _user) {
    		this.userDAO = UserDAO.getFromApplicationContext(this.getApplicationContext());
    		User user = userDAO.findById(_user.getId());
    		if (user != null) {
    			if (user.getPassword() == _user.getPassword()) {
    				return user;
    			}
    		}
    		return new User();
    	}
    
    	@Override
    	public void setApplicationContext(ApplicationContext _ctx)
    			throws BeansException {
    		UserServiceImpl.ctx = _ctx;
    	}
    	
    	public ApplicationContext getApplicationContext() {
    		return UserServiceImpl.ctx;
    	}
    }
    User DAO implemented by an entity User and a DAO UserDAO

    User.java
    Code:
    package vn.kbnn.dtkb.system.dtos;
    
    import java.io.Serializable;
    import java.util.Date;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;
    
    @SuppressWarnings("serial")
    @Entity
    @Table(name="DT_DM_USER",schema="DTKB", uniqueConstraints = {  })
    public class User implements Serializable{
    	@Id
    	@Column(name = "ID", nullable = false, insertable = true, updatable = false)
    	private String id;
    	@Column(name = "MAT_KHAU")
    	private String password;
    	@Column(name = "NGAY_CN")
    	@Temporal(TemporalType.DATE)
    	private Date updateDate;
    	@Column(name = "NGAY_DBO")
    	@Temporal(TemporalType.DATE)
    	private Date syncDate;
    	@Column(name = "NGAY")
    	private int date;
    	
    	public User() {
    		this.id = "";
    	}	
    	public User(String _id, String _password) {
    		this.id = _id;
    		this.password = _password;
    	}
    	public String getId() {
    		return id;
    	}
    	public void setId(String id) {
    		this.id = id;
    	}
    	public String getPassword() {
    		return password;
    	}
    	public void setPassword(String password) {
    		this.password = password;
    	}
    	public Date getUpdateDate() {
    		return updateDate;
    	}
    	public void setUpdateDate(Date updateDate) {
    		this.updateDate = updateDate;
    	}
    	public Date getSyncDate() {
    		return syncDate;
    	}
    	public void setSyncDate(Date syncDate) {
    		this.syncDate = syncDate;
    	}
    	public int getDate() {
    		return date;
    	}
    	public void setDate(int date) {
    		this.date = date;
    	}
    }
    UserDAO.java
    Code:
    package vn.kbnn.dtkb.system.dtos;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.orm.jpa.support.JpaDaoSupport;
    
    public class UserDAO extends JpaDaoSupport {
    
    	public void save(User entity) {
    	    try {
    	    	getJpaTemplate().persist(entity);
    	    } catch (RuntimeException re) {
    	        throw re;
            }
        }
        
        public void delete(User entity) {
    	    try {
            	entity = getJpaTemplate().getReference(User.class, entity.getId());
            	getJpaTemplate().remove(entity);
    	    } catch (RuntimeException re) {
    	        throw re;
            }
        }
        
        public User update(User entity) {
    	    try {
                User result = getJpaTemplate().merge(entity);
    	        return result;
            } catch (RuntimeException re) {
    	        throw re;
            }
        }
        
        public User findById(String id) {
    	    try {
                User instance = getJpaTemplate().find(User.class, id);
                return instance;
            } catch (RuntimeException re) {
    	        throw re;
            }
        }
    
    	public static UserDAO getFromApplicationContext(ApplicationContext ctx) {
    		return (UserDAO) ctx.getBean("userDAO");
    	}    
    }

    Comment


    • #3
      Here my problem:

      1. My calling to User web service is not success. Server don't get value of parameters. But if I have web service test tool provide by MyEclipse, server get inputs from browser. My code in LoginAction have what wrong?

      2. By using MyEclipse tools, the field "date" required.

      3. After deploy project, must be restart server or will get Gemspace (Out of memory error).

      4. First call to login.do, server idle till another call to login.do.

      5. Gemspace headache when running project.

      6. Seem all lib jar used when running project, even if I undeploy, they still in use that must stop server then delete files manually.

      My complete code is in the zip file attachmented.

      Please help me solve them.

      Thx

      Comment

      Working...
      X