SSH中使用annotation

 

在ssh整合的时候,感觉Hibrenate和Spring的Ioc使用annotation会好一点。这里,初步介绍一下是怎么用的。

 

少说多做,不解释,看代码:

 

1:hibernate.cfg.xml

 

     略

 

2:Struts.xml

 

  <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//ApacheSoftware Foundation//DTD Struts Configuration 2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd">
 
<struts>
<constant name="struts.objectFactory"value="spring"></constant>
<constantname="struts.devMode" value="true"></constant>
<package name="struts"extends="struts-default" namespace="/">
       <action name="UserAction" class="UserAction">
           <result>/success.jsp</result>
       </action>
   </package>
</struts>

3:applicationContext.xml  做的时候,这里有个错误让我很纠结:White spaces arerequired between publicId and systemId

 

可能是上面的schema写的不对吧!在网上找了好久,也没个好的解释。不过还好,知道对的方法就可以了。

 

<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="
           http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd">
             
   <bean id="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
       <property name="configLocation"
           value="classpath:hibernate.cfg.xml">
       </property>       
   </bean>
   <context:annotation-config/>
 
<!-- 上面的一行是让spring认识annotation,下面的一行是让它去org.annotation去扫描它需要的东西。-->
   <context:component-scan base-package="org.annotation" />
</beans>

4:java代码:

 

   a:User类

 

package org.annotation.domain;
import java.util.Date;
 
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
 
import org.hibernate.validator.Length;
 
@Entity
public class User {
   private int id;
   private String name;
   private String sex;
   private Date birthday;
   @Length(max=2)
   public String getSex() {
       return sex;
    }
   public void setSex(String sex) {
       this.sex = sex;
    }
   @Temporal(TemporalType.TIMESTAMP)
   public Date getBirthday() {
       return birthday;
    }
   public void setBirthday(Date birthday) {
       this.birthday = birthday;
    }
   @Id
   @GeneratedValue
   public int getId() {
       return id;
    }
   public void setId(int id) {
       this.id = id;
    }
   @Length(max=20)
   public String getName() {
       return name;
    }
   public void setName(String name) {
       this.name = name;
    }
}

================================

 

   b:UserDAO和UserService接口略,里面只有一个方法:void save(User user);

 

=====================================================

 

   c:UserDAOImpl代码: 

package org.annotation.dao.impl;
import javax.annotation.Resource;
import org.annotation.dao.UserDAO;
import org.annotation.domain.User;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
importorg.springframework.stereotype.Repository;
 
@Repository("userDao")
public class UserDAOImpl extendsHibernateDaoSupport implements UserDAO {
 
   @Resource(name = "sessionFactory")
   public void setSuperSessionFactory(SessionFactory sessionFactory) {
       super.setSessionFactory(sessionFactory);
    }
 
   public void save(User user) {
       this.getHibernateTemplate().save(user);
    }
 
}


 

===================================

 

d:UserServiceImpl代码:

 

package org.annotation.service.impl;
 
import org.annotation.dao.UserDAO;
import org.annotation.domain.User;
import org.annotation.service.UserService;
importorg.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImpl implementsUserService {
   private UserDAO userDao;   
 
   public UserDAO getUserDao() {
       return userDao;
    }
 
   @Autowired
   public void setUserDao(UserDAO userDao) {
       this.userDao = userDao;
    }
 
 
   public void save(User user) {
       this.userDao.save(user);
    }
 
}

======================================

 

e:UserAction代码

 

package org.annotation.action;
 
import org.annotation.domain.User;
import org.annotation.service.UserService;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.context.annotation.Scope;
importorg.springframework.stereotype.Component;
 
import com.opensymphony.xwork2.Action;
importcom.opensymphony.xwork2.ActionSupport;
 
@Component("UserAction")
@Scope("prototype")
public class UserAction extendsActionSupport {
 
   private static final long serialVersionUID = 1L;
   private User user;
   private UserService userService;
   public UserService getUserService() {
       return userService;
    }
   @Autowired
   public void setUserService(UserService userService) {
       this.userService = userService;
    }
   public User getUser() {
       return user;
    }
   public void setUser(User user) {
       this.user = user;
    }
   public String save(){
       this.userService.save(user);
       return Action.SUCCESS;
    }
}

=================================================

 

f:index.jsp核心代码:

 

 <s:form action="UserAction!save"namespace="/">
       <s:textfield name="user.name" label="帐号"></s:textfield>
       <s:radio list="{'男','女'}"name="user.sex" label="性别"></s:radio>
        <sx:datetimepicker name="user.birthday" displayFormat="yyyy-MM-dd"label="生日"/>
       <s:submit value="确定"></s:submit>
   </s:form>

===============================================

 

 g:success.jsp略

 

============================================

 

这样,就可以了!

你可能感兴趣的:(String,struts,ssh,user,import,Autowired)