Spring整合Struts2方式一:让Spring容器管理控制器

本文介绍了一个使用Spring框架实现的部门信息管理系统,包括web.xml配置、index.jsp页面、ok.jsp显示结果、Dept类、DeptDAO接口及其实现、AddDeptAction类以及struts.xml配置,展示了从页面交互到数据持久化的完整流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值