废话不多说了,直奔今天的主题吧!
1.首先来自浏览器客户端的请求:http://xx.xxxx.com/项目名称/search.html?id=12345
2.然后经过web.xml
<filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.html</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>
即所有以.html结尾的url都要经过FilterDispatcher的过滤,然后过滤器会根据url中的search分派到struts.xml文件。
注意:上面的REQUEST,FORWARD表示:
3.接着看struts.xml文件
<action name="search" class="vacationSearchAction" method="findById"> <result name="success">WEB-INF/jsp/common/string.jsp</result> </action>
由上面可以看到search对应着class为vacationSearchAction类里面的名为findById的方法。
注意:这里的class只是别名,实现类是在对应的spring文件里面:
4. spring-action.xml:
<bean id="vacationSearchAction"class="com.mangocity.vacation.query.action.SearchAction" scope="prototype"> <property name="vacationQueryService" ref="vacationQueryService" /> </bean>
这里有两点值得注意:(1)scope:spring默认生成的是单例。Singleton: Spring容器只存在一个共享的bean实例,是默认的配置 ,而 Prototype: 每次对bean的请求都会创建一个新的bean实例。
二者选择的原则:有状态的bean都使用Prototype作用域,而对无状态的bean则应该使用singleton作用域。而struts2传过来的action显然是有状态的,所以设置为prototype。
(2)这里注入了vacationQueryService,要到SearchAction中进行get/set
最后进入到SearchAction类里面的findById方法,处理完后返回success,便会跳转到string.jsp!