web项目:action调用service service调用dao
-
创建action类、service类、dao类
创建dao类
package xidian.lili.dao;
public class User {
public void add(){
System.out.println("dao...........");
}
}
创建service类,在service类中调用dao,设置引用类型属性User user;通过配置文件注入属性,所以添加user的set方法
package xidian.lili.service;
import xidian.lili.dao.User;
public class UserService {
private User user;
public void add(){
System.out.println("sevice...........");
}
public void setUser(User user) {
this.user = user;
user.add();
}
}
创建action类,在action类中获得service对象,通过加载spring配置文件,通过getBean()方法获得service对象,然后调用service方法
package xidian.lili.action;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.opensymphony.xwork2.ActionSupport;
import xidian.lili.service.UserService;
public class UserAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("action");
ApplicationContext context=
new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService=(UserService) context.getBean("userService");
userService.add();
return NONE;
}
}
-
完成spring配置文件和struts2配置文件
导入spring和struts2所需要的jar包
spring配置文件:在service对象中注入dao对象
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置对象 -->
<bean id="user" class="xidian.lili.dao.User"></bean>
<bean id="userService" class="xidian.lili.service.UserService">
<property name="user" ref="user"></property>
</bean>
</beans>
struts配置文件:配置action,访问userAction,创建userAction对象,在UserAction类中执行action方法调用
<?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.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true"/>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
<package name="front" namespace="/" extends="struts-default">
<action name="userAction" class="xidian.lili.action.UserAction">
</action>
</package>
</struts>
-
在web.xml文件中配置struts2过滤器和监听器
首先配置struts2过滤器,项目就可以部署运行,问题是每次执行都是加载spring配置文件,创建ApplicationContext对象,效率低
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>MyBBS</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 指定spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置监听器 -->
<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>
所以在服务器启动的时候加载spring配置文件,所以配置监听器<listener></listener>,创建sevletcontext对象这里需要导入下面的jar包
还要指定加载的spring配置文件的路径,因为不指定默认会在web-inf下面找,如下: