您的位置:首页 > 编程语言 > Java开发

spring,hibernate,struts2三大框架的整合秘籍

2017-12-09 16:08 459 查看
ssh三大框架的整合:
1.web.xml文件:
使用上下文参数指定spring配置文件的位置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>

配置spring框架的监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

配置过滤器,spring框架提供的解决hibernate延迟加载文件
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 配置struts2的核心过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

2.发送请求的几种方式:
1.通过超链接来发送请求:<a href="${pageContext.request.contextPath}/findAllCustomer">查询客户信息</a>
2.通过<form action="${pageContext.request.contextPath}/addCustomer" enctype="multipart/form-data" method ="post">
客户名称:<input type = "text" name = "cusName"><br>
客户电话:<input type = "text" name = "cusPhone"><br>
文件:<input type = "file" name = "cusImg"><br>
<input type = "submit" value = "addCustomer"><br>
</form>
3.通过window.location.href = "${pageContext.request.contextPath}/addCustomer.jsp";
4.异步校验
$.post(url,{x:x1,y:y1,...},function(data){

},'json');
5.ajax异步校验
$.ajax({
url:url,
type:'post',
data:{x:x1,y:y1,...},
dataType:'json',
success:function(data){
}
});

3.struts2映射文件的注解
@Entity //声明实体类,相当于<class> //类与表的映射
@Table(name = "t_customer",catalog = "sshtest")   //相当于<class
public class Customer {
@Id //主键映射
@GeneratedValue(strategy = GenerationType.IDENTITY) //主键生成策略
//如果id是String类型的就使用下面的注解来声明主键生成策略
//@GenericGenerator(name = "myuuid",strategy = "uuid")
//@GeneratedValue(generator = "myuuid")
private String orderNum;//订单编号
//属性映射
private Integer id; 
private String cusName;
private String cusPhone;
private  String cusImgSrc;
@ManyToOne(targetEntity = Customer.class)
@JoinColumn(name = "customer_id")//外键名称
private Customer c;//一个订单只属于一个客户
@ManyToOne(targetEntity一方的.class)
@JoinColumn(name="多方中一方的外键(表里面的列名)")
@OneToOne

@ManyToMany(targetEntity=对方的.class,mappedBy="对方中己方的属性")
@ManyToMany(targetEntity=对方的.class)
@JoinTable(name="",joinColumns={@JoinColumn(name="")},inverseJoinColumns={@JoinColumn(name="")})
}
//struts2的映射配置文件
<hibernate-mapping>
//table表名,catalog是指的数据库
<class name= "com.itheima.ssh.domain.User" table="t_user" >
<id name = "id"> 
<generator class="native"></generator>
</id>
<property name="name"></property>
<property name="age"></property>
</class>
</hibernate-mapping>

4.struts.xml的注解
在action类中来进行注解
@Controller  //专门在action层使用
@Namespace("/")  //
@ParentPackage("struts-default")
@Scope("prototype")
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
struts.xml
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="" class="" method="">
//name指的是请求 class有两种方式:spring整合了struts后这个class中可以写配置文件中的bean 的id的值,也可以写全路径名,具体看情况
<result name = "" type = "">页面</result>
</action>
</package>
</struts>

5.几大注解的使用
@Controller     @Service("CustomerService")       @Repository("CustomerDao")       @Component

      action          service                     
dao                            在哪个层都适用
  注意:@Transactional必须在service层开启 
@value  //简单的属性注入
@Autowired   //根据属性注入
@Autowired("userDao") 和@Qualifier一起使用可以根据名称注入
@Resource(name = "需要注入的属性的名称")

6.三大框架的整合的大概步骤
(1):struts2负责接收页面传递来的参数,请求然后转发到业务层进行处理,然后将处理结果返回给
afbd
用户
(2):在action层中需要继承ActionSupport(目的就是创建action类,),实现ModelDriven接口(接收页面传递来的数据并且封装),调用service
(3):在service层中需要调用dao,
(4):dao层继承hibernateDaoSupport,然后再注入一个sessionFactroy
@Autowired
@Qualifier("sessionFactory")
public void setSuperSessionFactory(SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}

7.接收页面传递来的数据
属性驱动
private String username;
name="username"
private User user;
name="user.username"
//setter  getter
模型驱动
Action implemnts ModelDriven<User>{
name="username"
private User user = new User();
public User getModel(){
return user;
}
}

8.applicationContext.xml中
<!--开启扫描注解 -->
<context:component-scan base-package="com.itheima"></context:component-scan>
<!--引入外部的db.properties文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 开启事务管理的注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

下面的是使用xml的方式
<!--声明事务管理  -->
<!-- 声明 管理器-->
<bean id = "transactionManager" class = "org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref = "sessionFactory"></property>

</bean>
<!--通知  -->
<tx:advice id = "txAdvice" transaction-manager = "transactionManager">
<tx:attributes>
<tx:method name="add"/>
<tx:method name="update"/>
<tx:method name="delete"/>
<tx:method name="find*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.itheima.ssh.service.UserServiceImpl.*(..))" id="mypointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypointcut"/>
</aop:config>

加载struts的映射文件
<property name="packagesToScan">
<list>
<value>yang.zhao.li.exam.domain</value>
</list>
</property>

9.存取值然后再页面获取到
1.把数据存放在valueStack中
List<User> list = userService.findAll();
ServletActionContext.getContext().getValueStack().set("list", list);
2.在页面获取值使用ognl表达式
<s:iterator value="list" status="s">
<tr>
<td>${s.count }</td>
<td>${name}</td>
<td>${sex }</td>
<td>${age }</td>
</tr>
</s:iterator>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息