首页 > 代码库 > spring、struts整合

spring、struts整合

package com.hanqi.test;

public class JISQ {

	public double add(double a,double b)
	{
		return (a+b);
	}

}

  

package com.hanqi.test;

public class TestDAO {
    //数据连接
    private String conn;
    
    public String getConn() {
        return conn;
    }

    public void setConn(String conn) {
        this.conn = conn;
    }

    public String getname()
    {
        return "得到连接="+conn+"并嗲用了DAO";
    }

}
package com.hanqi.test;

public class TestService {

	private TestDAO ts;
	public TestDAO getTs() {
		return ts;
	}
	public void setTs(TestDAO ts) {
		this.ts = ts;
	}
	public String getDAOname()
	{
		//TestDAO td=new TestDAO();
		return ts.getname();
				
	}
}

  

package com.hanqi.test;

 
public class TestAction 
{
	private TestService ts7;
	public TestService getTs7() {
		return ts7;
	}
	public void setTs7(TestService ts7) {
		this.ts7 = ts7;
	}
	public String test1()
	{
		//TestService ts=new TestService();
		System.out.println(ts7.getDAOname());
		return "success";
	}
	


}

  

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

<bean id="jsq" class="com.hanqi.test.JISQ"></bean>


<!--Action类的实例不能是单例的  -->
<bean id="test2" class="com.hanqi.test.TestAction" scope="prototype"></bean>

<bean id="testDAO" class="com.hanqi.test.TestDAO">

<property name="conn" value="http://www.mamicode.com/Oracle"></property>

</bean>
<bean id="testService" class="com.hanqi.test.TestService">
<property name="ts" ref="testDAO"></property>
</bean>
<bean id="testID" class="com.hanqi.test.TestAction">
<property name="ts7" ref="testService"></property>

</bean>

</beans>

  

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<!-- 设置过滤的扩展名 -->
<constant name="struts.action.extension" value="http://www.mamicode.com/action,do,,"></constant>
<!-- 允许调用静态方法和静态属性 -->
<constant name="struts.ognl.allowStaticMethodAccess" value="http://www.mamicode.com/true"></constant>
<!-- 定义包 -->
<package name="test" extends="struts-default">
<action name="testform" class="testID" method="test1">
<result>view.jsp</result>
</action>

</package>
</struts>

  

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Test39</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- spring有关的 -->
  
  
  <!-- 配置文件 -->
  <!-- needed for ContextLoaderListener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:app.xml</param-value>
	</context-param>

	<!-- Bootstraps the root web application context before servlet initialization -->
	<!-- 把Spring容器放到全局对象中 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
  
  <!-- 过滤器 -->
   <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>
  
  
  
  
  
</web-app>

  

<%@page import="com.hanqi.test.JISQ"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
测试

<%
//调用spring容器的bean的实例

//加载容器  通过什么途径
WebApplicationContext ac=
WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());

JISQ jsq=ac.getBean(JISQ.class);






%>
<br>
123+456=<%=jsq.add(123, 456)%>

<br><br>
<a href="http://www.mamicode.com/testform">发起请求</a>
</body>
</html>

  

spring、struts整合