web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
index.jsp :
<body>
<form action="add" method="post">
部门名称:<input type="text" name="dname"><br>
部门地址:<input type="text" name="loc"><br>
<input type="submit" value="提交">
</form>
</body>
ok.jsp :
<body>
部门信息添加成功...
</body>
Dept.java :
public class Dept {
private String dname;
private String loc;
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
DeptDAO.java :
public interface DeptDAO {
public void save(Dept dept);
}
DeptDAOImpl.java :
public class DeptDAOImpl implements DeptDAO {
@Override
public void save(Dept dept) {
System.out.println("将Dept对象保存进数据库");
}
}
AddDeptAction.java :
public class AddDeptAction extends ActionSupport {
private String dname;
private String loc;
private DeptDAO deptDao;
public String execute(){
Dept dept=new Dept();
dept.setDname(dname);
dept.setLoc(loc);
deptDao.save(dept);
return "success";
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
public void setDeptDao(DeptDAO deptDao) {
this.deptDao = deptDao;
}
}
struts.xml :
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="demo" extends="struts-default">
<action name="add" class="addDeptAction">
<result>/ok.jsp</result>
</action>
</package>
</struts>
beans.xml :
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="addDeptAction" class="com.action.AddDeptAction" scope="prototype">
<property name="deptDao" ref="dao"/>
</bean>
<bean id="dao" class="com.dao.DeptDAOImpl"/>
</beans>