ssh2的重要代码

 <!-- web.xml核心代码 -->
  <!-- 初始化webApplicationContext(相当于beanfactory),
  listener将其放入了servletcontext中 -->
   <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
 </context-param>
  <!-- 配置struct的拦截器 -->
  <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>

//hibernate提供的	API
package com.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.bigjava.bean.TTestPaper;

public class HibernateTest {
public static void main(String[] args) {

Configuration config=new Configuration().configure();
SessionFactory factory=config.buildSessionFactory();
Session s = factory.openSession();
Transaction tx = s.beginTransaction();
s.save(new TTestPaper("1","1",1,"1"));
tx.commit();
s.close();
factory.close();
}
}

<!-- spring的核心配置 -->
 <!-- 配置transactionManager -->
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
 <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>

  <!-- 定义事务处理规则 -->
<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>

 <!-- 配置AOP -->
 <aop:config proxy-target-class="true">
 <!--定义切面 -->
 <aop:pointcut expression="execution(* com.bigjava.dao.*.*(..))" id="point" />
 <!--定义织入点规则  -->
 <aop:advisor advice-ref="tx" pointcut-ref="point" />
 </aop:config>
 
</beans>

/*springTest*/
package com.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import com.bigjava.dao.TTestPaperDao;

public class SpringTest {
private TTestPaperDao tTestPaperDao;

public void settTestPaperDao(TTestPaperDao tTestPaperDao) {
	this.tTestPaperDao = tTestPaperDao;
}
public void test(){
	System.out.println("list--size-->"+tTestPaperDao.findall().size());
}
public static void main(String[] args) {
	//类似于sessionfactory
	ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
	SpringTest test=(SpringTest)context.getBean("springTest");
	test.test();
}
}

<!-- structs核心配置 -->
<struts>
     <!-- 让spring来管理struct2中的action -->
     <constant name="struts.objectFactory" value="spring" />
	<package name="com.test" namespace="/test" extends="struts-default" >
		<action name="StructTest" class="structTest">
			<result name="success">/welcome.jsp</result>
			<result name="hello">/welcome1.jsp</result>
			
		</action>
		<action name="ExcelContrlloer" class="com.test.ExcelContrlloer">
			<result name="excletest" type="xslt">/welcome.jsp</result>
		</action>
	</package>
</struts> 

/*structTest*/
package com.test;

import org.springframework.stereotype.Controller;

import com.bigjava.bean.TTestPaper;
import com.bigjava.dao.TTestPaperDao;
import com.opensymphony.xwork2.ActionSupport;
@Controller("structTest")
public class StructTest extends ActionSupport{
	private Login p=new Login();	
	@Override
	public String execute() throws Exception {
		//System.out.println("actiontest success...");
		return 	"";
	}
	public String StructTest() throws Exception {
		/*
		 * 如同普通的servlet一样,得到beanfactory
		HttpServletRequest request = ServletActionContext.getRequest();
		ServletContext sc=request.getSession().getServletContext();
		ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
		tTestPaperDao = (TTestPaperDao) ac.getBean("tTestPaperDao");
		*/
		//System.out.println("size--->"+tTestPaperDao.findall().size());
	    System.out.println(p.getPaperId()+"fdfd"+p.getPaperName());
		return 	SUCCESS;
	}
	public String StructTest1() throws Exception {
		System.out.println("size--->"+tTestPaperDao.findall().size());
		return 	"hello";
	}
	 private TTestPaperDao tTestPaperDao;
		public void settTestPaperDao(TTestPaperDao tTestPaperDao) {
		this.tTestPaperDao = tTestPaperDao;
	}
	
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值