开发中理解框架

最近在学习MVC框架时,一直对于框架的三层分布使用感觉到很迷茫,最近在开发项目中,自己有明白了很多,所以整合了自己的开发思想和总结,
希望能帮助跟我一样迷茫的“菜鸟”,如果中间出现错误,还希望大佬们多多指教,及时得到改正。
1、一般在jsp/html中的表单数据的<form id=””action=””.>
<form id="addStaffForm" action="staffAction_add.action" method="post">
action属性值一般都是页面跳转到struts.xml中查找相应的《action name=””》的属性值;如果没有添加action中name属性值,页面则会报错:
There is no Action mapped for namespace [/] and action name [staffAction_add] associated with context path [/bos-web].

2.创建action类:完成页面创建后,在相应的包中创建相应的action类,定义service层接口以及实现类,定义Dao层接口以及相应的实现类;注意在接口实现类中添加注解
在action(Controller)中调用service层:
注解:action(controller层)
    @Controller
    @Scope(“protiotype”)
在service层中调用Dao层:
注解:service层加入注解
	@Service
	@Transactional
在Dao层操作数据:  进行数据的增删改查操作
    注解: dao层加入注解
	@Reposity

	Action中的方法实现:
首先加入注解
	@Controller
	@Scope(“prototype”)
	…………
	@Autowried
	Private xxxService  xxxx;//声明service层的对象
	在方法中定义service,在action中调用service层
定义实现方法:
使用service层的对象调用service层中定义的方法;
Service层的方法实现:
加入以上所需注解:在接口实现类中加入注解
	Private  xxxDao  xxxDao;//声明Dao层对象
	定义实现Service(Action层调用的方法)方法:
使用Dao层对象调用Dao层方法;
Dao层方法的实现:
加入以上所说的注解:接口实现类中添加注解方式
实现Dao层接口,在实现类中定义方法(即service层调用的方法)
定义操作数据库的HQL语句,SQL语句等,实现操作的目的;
最后实现以上所需方法后,
在struts.xml 中定义xx.jsp传过来的action路径


整合:
由前端页面(xxx.jsp)访问action路径,然后在action类中定义前端操作所需要实现的操作(比如:前端添加用户,action类中定义add())方法,
然后调用service层----》dao层,在Dao层定义具体方法实现操作在struts.xml中定义action访问路径

	





猜你喜欢

转载自blog.csdn.net/xuan_lu/article/details/79919557