1.struts2入门

*   struts2并不是struts1的升级版

*   是由webwork基础发展起来的

*   吸收了struts1和webwork两者的优势

*   javaweb四层架构  显示层--------控制层---------业务层-----------持久化层  

1.struts2框架工作原理

1.1.struts2框架的模拟

*   是基于过滤器的

public class Struts2Filter implements Filter {
	Map<String, String> map = new HashMap<String, String>();
	public void init(FilterConfig config) throws ServletException {
		map.put("/primer/userAction.action", "cn.itcast.action.UserAction");
		map.put("/helloWorld/helloWorldAction.action", "cn.itcast.action.HelloWorldAction");
	}
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		//强转
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse res = (HttpServletResponse)response;
		String path = req.getServletPath();
		System.out.println("path = "+path);
		if(path.equals("/test.jsp")){
			chain.doFilter(req, res);
		}else{
			try {
				Action action;
				//利用反射,通过newInstance()得到实例化
				action = (Action)Class.forName(map.get(path)).newInstance();
				action.execute();
				req.getRequestDispatcher("/success.jsp").forward(req, res);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	public void destroy() {
	}
*   req.getServletPath() 得到的是出去项目名的请求路径

 入门的路径:<br>  
      <a href="${pageContext.request.contextPath}/primer/userAction.action">userWorld</a><br>
      <br>
      <a href="${pageContext.request.contextPath}/helloWorld/helloWorldAction.action">helloWorld</a><br>
      http://127.0.0.1:8080/itcast1105_struts2moni/test.jsp
*   得到的是  /primer/userAction.action   /helloWorld/helloWorldAction.action     /test.jsp

*   接口  多态  反射
1.2.struts2框架

*   开发struts2需要的框架


*   这里使用的是2.3.3版本

*   struts2配置文件struts.xml名字是默认的  没有大写  默认是放在src下面的

<struts>
	<!-- /primer/helloWorldAction.action
		package:包
			* name:包名,唯一的,必选项
			* namespace:命名空间,唯一的,相当于房间号。可选项,省略情况下是"/"。页面中请求连接的前半部分
			* extends:继承
				* extends="struts-default":struts2框架底层提供的核心包struts2-core-2.3.3.jar下的struts-default.xml文件
				* 为什么要继承这个struts-default.xml文件?
	 -->
	<package name="primer" namespace="/primer" extends="struts-default">
		<!-- 
			action:
				* name:对应页面中请求连接的后面半部分
				* class:对应要执行的类的完整路径
		 -->
		<action name="helloWorldAction" class="cn.itcast.primer.HelloWorldAction">
			<!-- 
				result:结果类型
					* name:对应的是执行的类的方法的返回值
						public String execute() throws Exception {
							System.out.println("HelloWorldAction ************* execute()");
							return "success";
						}
					* 后半部分的文本内容:要转向到的页面
			 -->
			<result name="success">/primer/success.jsp</result>
		</action>
	</package>
</struts>
*   struts.xml里面的配置

*   struts2框架的工作流程

    tomcat------加载web.xml-------web.xml------加载过滤器(struts2框架提供的过滤器,但是程序员需要在web.xml文件中进行配置)------加载struts.xml



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值