首页 > 学院 > 开发设计 > 正文

第28天(就业班) 自定义mvc框架、Struts入门及执行流程、环境搭建

2019-11-08 02:35:24
字体:
来源:转载
供稿:网友
1.自定义mvc框架写一个mystruts框架Mvc模式:Model 模型 View 试图 Control 控制器Control,  控制器Servlet起到控制器作用!----》 获取请求数据封装【BeanUtils可以优化,(调用方法?)】----》 调用Service处理业务逻辑        ----》 跳转(转发/重定向)              【跳转代码写死】传统mvc开发总结:1. 跳转代码写死,不灵活2. 每次都去写servlet,web.xml中配置servlet!

(配置目的: 请求, Servlet处理类)

package com.xp.entity;public class User {	PRivate String name;	private String pwd;	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	public String getPwd() {		return pwd;	}	public void setPwd(String pwd) {		this.pwd = pwd;	}}package com.xp.dao;import com.xp.entity.User;/** * 用户登陆、注册 */public class UserDao {	// 模拟登陆	public User login(User user){		if ("tom".equals(user.getName()) && "888".equals(user.getPwd()) ){			// 登陆成功			return user;		}		// 登陆失败		return null;	}		// 模拟注册	public void register(User user) {		System.out.println("注册成功:用户," + user.getName());	}}package com.xp.service;import com.xp.dao.UserDao;import com.xp.entity.User;public class UserService {	private UserDao ud = new UserDao();	// 模拟登陆	public User login(User user){		return ud.login(user);	}		// 模拟注册	public void register(User user) {		ud.register(user);	}}package com.xp.framework.action;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.xp.entity.User;import com.xp.service.UserService;/** * Action表示动作类 1. 一个servlet对应一个action 2. action中负责处理具体的请求*  */public class LoginAction {		public Object execute(HttpServletRequest request, HttpServletResponse response)	throws ServletException, IOException {		return null;	}	/**	 * 处理登陆请求	 */	public Object login(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {		Object uri = null;		// 1. 获取请求数据,封装		String name = request.getParameter("name");		String pwd = request.getParameter("pwd");		User user = new User();		user.setName(name);		user.setPwd(pwd);		// 2. 调用Service		UserService userService = new UserService();		User userInfo = userService.login(user);		// 3. 跳转		if (userInfo == null) {			// 登陆失败//			request.getRequestDispatcher("/login.jsp").forward(request,//					response);//			uri = request.getRequestDispatcher("/login.jsp");			uri = "loginFaild";   // loginFaild  = /login.jsp		} else {			// 登陆成功			request.getsession().setAttribute("userInfo", userInfo);			// 首页//			response.sendRedirect(request.getContextPath() + "/index.jsp");//			uri = "/index.jsp";			uri = "loginSuccess";  // loginSuccess = /index.jsp		}		return uri;	}}package com.xp.framework.action;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.xp.entity.User;import com.xp.service.UserService;public class RegisterAction {	public Object register(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {		Object uri;		// 1. 获取请求数据,封装		String name = request.getParameter("name");		String pwd = request.getParameter("pwd");		User user = new User();		user.setName(name);		user.setPwd(pwd);		// 2. 调用Service		UserService userService = new UserService();		userService.register(user);		// 3. 跳转//		request.getRequestDispatcher("/login.jsp").forward(request, response);		//uri = request.getRequestDispatcher("/login.jsp");		return "registerSuccess"; //返回注册的标记;   registerSuccess = /login.jsp	}}package com.xp.framework.bean;public class Result {	// 跳转的结果标记		private String name;		// 跳转类型,默认为转发; "redirect"为重定向		private String type;		// 跳转的页面		private String page;		public String getName() {			return name;		}		public void setName(String name) {			this.name = name;		}		public String getType() {			return type;		}		public void setType(String type) {			this.type = type;		}		public String getPage() {			return page;		}		public void setPage(String page) {			this.page = page;		}	}package com.xp.framework.bean;import java.util.Map;/** * 封装action节点 *      <action name="login" class="com.xp.framework.action.LoginAction" method="login">			<result name="success" type="redirect">/index.jsp</result>			<result name="loginFaild">/login.jsp</result>		</action>		* */public class ActionMapping {	// 请求路径名称	private String name;	// 处理aciton类的全名	private String className;	// 处理方法	private String method;	// 结果视图集合	private Map<String,Result> results;		public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	public String getClassName() {		return className;	}	public void setClassName(String className) {		this.className = className;	}	public String getMethod() {		return method;	}	public void setMethod(String method) {		this.method = method;	}	public Map<String, Result> getResults() {		return results;	}	public void setResults(Map<String, Result> results) {		this.results = results;	}	}package com.xp.framework.bean;import java.io.InputStream;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.io.SAXReader;/** * 加载配置文件, 封装所有的真个mystruts.xml * */public class ActionMappingManager {	// 保存action的集合	private Map<String,ActionMapping> allActions ;		public ActionMappingManager(){		allActions = new HashMap<String,ActionMapping>();		// 初始化		this.init();	}		/**	 * 根据请求路径名称,返回Action的映射对象	 * @param actionName   当前请求路径	 * @return             返回配置文件中代表action节点的AcitonMapping对象	 */	public ActionMapping getActionMapping(String actionName) {		if (actionName == null) {			throw new RuntimeException("传入参数有误,请查看struts.xml配置的路径。");		}				ActionMapping actionMapping = allActions.get(actionName);		if (actionMapping == null) {			throw new RuntimeException("路径在struts.xml中找不到,请检查");		}		return actionMapping;	}		// 初始化allActions集合	private void init() {		/********DOM4J读取配置文件***********/		try {			// 1. 得到解析器			SAXReader reader = new SAXReader();			// 得到src/mystruts.xml  文件流			InputStream inStream = this.getClass().getResourceAsStream("/mystruts.xml");			// 2. 加载文件			Document doc = reader.read(inStream);						// 3. 获取根			Element root = doc.getRootElement();						// 4. 得到package节点			Element ele_package = root.element("package");						// 5. 得到package节点下,  所有的action子节点			List<Element> listAction = ele_package.elements("action");						// 6.遍历 ,封装			for (Element ele_action : listAction) {				// 6.1 封装一个ActionMapping对象				ActionMapping actionMapping = new ActionMapping();				actionMapping.setName(ele_action.attributeValue("name"));				actionMapping.setClassName(ele_action.attributeValue("class"));				actionMapping.setMethod(ele_action.attributeValue("method"));								// 6.2 封装当前aciton节点下所有的结果视图				Map<String,Result> results = new HashMap<String, Result>();								// 得到当前action节点下所有的result子节点				 Iterator<Element> it = ele_action.elementIterator("result");				 while (it.hasNext()) {					 // 当前迭代的每一个元素都是 <result...>					 Element ele_result = it.next();					 					 // 封装对象					 Result res = new Result();					 res.setName(ele_result.attributeValue("name"));					 res.setType(ele_result.attributeValue("type"));					 res.setPage(ele_result.getTextTrim());					 					 // 添加到集合					 results.put(res.getName(), res);				 }								// 设置到actionMapping中				actionMapping.setResults(results);								// 6.x actionMapping添加到map集合				allActions.put(actionMapping.getName(), actionMapping);			}								} catch (Exception e) {			throw new RuntimeException("启动时候初始化错误",e);		}	}}package com.xp.framework;import java.io.IOException;import java.lang.reflect.Method;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.xp.framework.bean.ActionMapping;import com.xp.framework.bean.ActionMappingManager;import com.xp.framework.bean.Result;/** * 核心控制器,此项目只有这一个servlet * 1. 拦截所有的*.action为后缀的请求 * 2. 请求:http://localhost:8080/mystruts/login.action * 		  http://localhost:8080/mystruts/register.action */public class ActionServlet extends HttpServlet{	private ActionMappingManager actionMappingManager;		// 只执行一次  (希望启动时候执行)	@Override	public void init() throws ServletException {		System.out.println("1111111111111111ActionServlet.init()");		actionMappingManager = new ActionMappingManager();	}	// http://localhost:8080/mystruts/login.action	@Override	protected void doGet(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {				try {			// 1. 获取请求uri, 得到请求路径名称   【login】			String uri = request.getRequestURI();			// 得到 login			String actionName=uri.substring(uri.lastIndexOf("/")+1, uri.indexOf(".action"));						// 2. 根据路径名称,读取配置文件,得到类的全名   【cn..action.LoginAction】			ActionMapping actionMapping = actionMappingManager.getActionMapping(actionName);			String className = actionMapping.getClassName();						// 当前请求的处理方法   【method="login"】			String method = actionMapping.getMethod();						// 3. 反射: 创建对象,调用方法; 获取方法返回的标记			Class<?> clazz = Class.forName(className);			Object obj = clazz.newInstance();  //LoginAction loginAction = new LoginAction();			Method m = clazz.getDeclaredMethod(method, HttpServletRequest.class,HttpServletResponse.class );			// 调用方法返回的标记			String returnFlag =  (String) m.invoke(obj, request, response);						// 4. 拿到标记,读取配置文件得到标记对应的页面 、 跳转类型			Result result = actionMapping.getResults().get(returnFlag);			// 类型			String type = result.getType();			// 页面			String page = result.getPage();						// 跳转			if ("redirect".equals(type)) {				response.sendRedirect(request.getContextPath() + page);			} else {				request.getRequestDispatcher(page).forward(request, response);			}		} catch (Exception e) {			e.printStackTrace();		}	}			@Override	protected void doPost(HttpServletRequest req, HttpServletResponse resp)			throws ServletException, IOException {		doGet(req, resp);	}}package com.xp.servlet;import java.io.IOException;import javax.servlet.RequestDispatcher;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.xp.framework.action.LoginAction;// 控制器public class LoginServlet extends HttpServlet {	public void doGet(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {		// 创建Action对象,调用登陆方法		LoginAction loginAction = new LoginAction();		Object uri = loginAction.login(request, response);		// 跳转		if (uri instanceof String) {			response.sendRedirect(request.getContextPath() + uri.toString());		} else {			((RequestDispatcher) uri).forward(request, response);		}	}	public void doPost(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {		this.doGet(request, response);	}}package com.xp.servlet;import java.io.IOException;import javax.servlet.RequestDispatcher;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.xp.framework.action.RegisterAction;public class RegisterServlet extends HttpServlet {	public void doGet(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {		RegisterAction registerAction = new RegisterAction();		Object uri = registerAction.register(request, response);				// 配置文件---》jsp				// 跳转		if (uri instanceof String) {			response.sendRedirect(request.getContextPath() + uri.toString());		} else {			((RequestDispatcher)uri).forward(request, response);		}	}	public void doPost(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {		this.doGet(request, response);	}}<?xml version="1.0" encoding="UTF-8"?><mystruts>	<package>		<!-- 配置请求路径,与处理action类的关系 -->		<!-- 			1. 请求路径与处理Action的关系			     /login = LoginAction                          login			            success = /index.jsp                     登陆成功(重定向)			            loginFaild  = /login.jsp                 登陆失败		 -->		<action name="login" class="com.xp.framework.action.LoginAction" method="login">			<result name="loginSuccess" type="redirect">/index.jsp</result>			<result name="loginFaild">/login.jsp</result>		</action>				<action name="register" class="com.xp.framework.action.RegisterAction" method="register">			<result name="registerSuccess">/login</result>		</action>	</package></mystruts><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>index</title>	<meta http-equiv="pragma" content="no-cache">	<meta http-equiv="cache-control" content="no-cache">	<meta http-equiv="expires" content="0">    	<meta http-equiv="keyWords" content="keyword1,keyword2,keyword3">	<meta http-equiv="description" content="This is my page">	<!--	<link rel="stylesheet" type="text/CSS" href="styles.css">	-->  </head>  <body>    欢迎你,${sessionScope.userInfo.name }  </body></html><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>        <title>login</title>	<meta http-equiv="pragma" content="no-cache">	<meta http-equiv="cache-control" content="no-cache">	<meta http-equiv="expires" content="0">    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">	<meta http-equiv="description" content="This is my page">	<!--	<link rel="stylesheet" type="text/css" href="styles.css">	-->  </head>    <body>   	<form action="${pageContext.request.contextPath }/login.action" name="frmLogin"  method="post">   	   用户名: <input type="text" name="name"> <br/>   	 密码: <input type="text" name="pwd"> <br/>   	   <input type="submit" value="登陆"> <br/>   	</form>  </body></html><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>        <title>login</title>	<meta http-equiv="pragma" content="no-cache">	<meta http-equiv="cache-control" content="no-cache">	<meta http-equiv="expires" content="0">    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">	<meta http-equiv="description" content="This is my page">	<!--	<link rel="stylesheet" type="text/css" href="styles.css">	-->  </head>    <body>   	<form action="${pageContext.request.contextPath }/register.action" name="frmRegister"  method="post">   	   用户名: <input type="text" name="name"> <br/>   	  密码: <input type="text" name="pwd"> <br/>   	   <input type="submit" value="注册"> <br/>   	</form>  </body></html>Web.Xml<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">  <display-name>mystruts</display-name>  <!-- 核心控制器 -->  <servlet>    <servlet-name>ActionServlet</servlet-name>    <servlet-class>com.xp.framework.ActionServlet</servlet-class>    <!-- 启动时候执行servlet初始化方法 -->    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>ActionServlet</servlet-name>    <url-pattern>*.action</url-pattern>  </servlet-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>2.struts第一个案例基于mvc模式的应用框架之strutsStruts就是基于mvc模式的框架!(struts其实也是servlet封装,提高开发效率!)Struts开发步骤:1. web项目,引入struts - jar包

2. web.xml中,引入struts的核心功能配置过滤器

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">  <display-name>struts20150313</display-name>  <!-- 引入struts核心过滤器 -->  <filter>  	<filter-name>struts2</filter-name>  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.class</filter-class>  </filter>  <filter-mapping>  	<filter-name>struts2</filter-name>  	<url-pattern>/*</url-pattern>  </filter-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>3. 开发action

package com.xp.action;import com.opensymphony.xwork2.ActionSupport;//开发action: 处理请求public class HelloAction extends ActionSupport{	// 处理请求		public String execute() throws Exception {			System.out.println("访问到了action,正在处理请求");			System.out.println("调用service");			return "success";		}}
4. 配置action	src/struts.xml<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts SYSTEM "http://struts.apache.org/dtds/struts-2.0.dtd" PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"><struts>	<package extends="struts-default" name="xxxx">	<action name="hello" method="execute" class="com.xp.action.HelloAction">	<result name="success">/success.jsp</result>	</action>	</package></struts>

3.struts详解

SSH框架在mvc模式的的位置作用:

框架:软件中的框架,是一种半成品; 我们项目开发需要在框架的基础上进行!因为框架已经实现了一些功能,这样就可以提高开发效率!b. Struts2框架Struts1最早的一种基于mvc模式的框架;Struts2 是在Struts1的基础上,融合了xwork的功能;也可以说,Struts2 = struts1  +  xworkStruts2框架预先实现了一些功能:1. 请求数据自动封装2. 文件上传的功能3. 对国际化功能的简化4. 数据效验功能4.Struts2开发流程 引入jar文件commons-fileupload-1.2.2.jar  【文件上传相关包】commons-io-2.0.1.jarstruts2-core-2.3.4.1.jar           【struts2核心功能包】xwork-core-2.3.4.1.jar           【Xwork核心包】ognl-3.0.5.jar 【Ognl表达式功能支持表】commons-lang3-3.1.jar          【struts对java.lang包的扩展】freemarker-2.3.19.jar            【struts的标签模板库jar文件】javassist-3.11.0.GA.jar           【struts对字节码的处理相关jar】 配置web.xmlTomcat启动- 加载自身web.xml---加载所有项目的web.xml通过在项目的web.xml中引入过滤器,-Struts的核心功能的初始化,通过过滤器完成 filter 【init/      启动执行doFilter/   访问执行destroy】

<!-- 引入struts核心过滤器 -->	<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>struts2-core-2.3.4.1.jarStrutsPrepareAndExecuteFilter  即为核心过滤器注意:使用的struts的版本不同,核心过滤器类是不一样的! 开发Action注意:1. action类,也叫做动作类; 一般继承ActionSupport类   即处理请求的类  (struts中的action类取代之前的servlet)2. action中的业务方法,处理具体的请求- 必须返回String  方法不能有参数

public class HelloAction extends ActionSupport {	// 处理请求	public String execute() throws Exception {}}配置struts.xml<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts SYSTEM "http://struts.apache.org/dtds/struts-2.0.dtd" PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"><struts>	<package extends="struts-default" name="xxxx">	<action name="hello" method="execute" class="com.xp.action.HelloAction">	<result name="success">/success.jsp</result>	</action>	</package></struts>

5.Struts2执行流程

c.Struts2执行流程

服务器启动:

    1.加载项目web.xml

    2.创建Struts核心过滤器对象,执行filter   init()

       struts-default.xml,    核心功能的初始化

struts-plugin.xml,     struts相关插件

struts.xml           用户编写的配置文件

访问:

    3.用户访问Action, 服务器根据访问路径名称,找对应的aciton配置, 创建action对象

    4.执行默认拦截器栈中定义的18个拦截器

    5.执行action的业务处理方法

6.struts-default.xml,详解

 目录:struts2-core-2.3.4.1.jar/ struts-default.xml

 内容:

    1.bean节点指定struts在运行的时候创建的对象类型

    2.指定struts-default包  【用户写的package(struts.xml)一样要继承此包】

       package  struts-default  包中定义了:

           a.  跳转的结果类型

              dispatcher    转发,不指定默认为转发

              redirect       重定向

              redirectAction  重定向到action资源

              stream        (文件下载的时候用)

           b.定义了所有的拦截器

                定义了32个拦截器!

                为了拦截器引用方便,可以通过定义栈的方式引用拦截器,

               此时如果引用了栈,栈中的拦截器都会被引用!

             

              defaultStack

                  默认的栈,其中定义默认要执行的18个拦截器!

           c.默认执行的拦截器栈、默认执行的action

<default-interceptor-ref name="defaultStack"/>               <default-class-ref class="com.opensymphony.xwork2.ActionSupport" /><interceptor name="prepare" class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/><interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>

拦截器(先睹为快):

拦截器功能与过滤器功能类似。

区别:

共同点:都拦截资源!

区别:

过滤器,拦截器所有资源都可以; (/index.jsp/servlet/img/css/js)

拦截器,只拦截action请求。

拦截器是struts的概念,只能在struts中用。

过滤器是servlet的概念,可以在struts项目、servlet项目用。

//面试题: 拦截器什么时候执行?(访问/启动)  先执行action类创建,先执行拦截器?

    // --》 1. 用户访问时候按顺序执行18个拦截器;

    //---》 2. 先执行Action类的创建,再执行拦截器; 最后拦截器执行完,再执行业务方法

package com.xp.b_action;public class User {	private String userName;	private String pwd;	public String getUserName() {		return userName;	}	public void setUserName(String userName) {		this.userName = userName;	}	public String getPwd() {		return pwd;	}	public void setPwd(String pwd) {		this.pwd = pwd;	}}package com.xp.b_action;import java.util.Map;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport{	// 面试题: 拦截器什么时候执行? (访问/启动)  先执行action类创建,先执行拦截器?	// --》 1. 用户访问时候按顺序执行18个拦截器;	//---》 2. 先执行Action类的创建,再执行拦截器; 最后拦截器执行完,再执行业务方法	public UserAction() {		System.out.println("UserAction.enclosing_method()");	}	/**	 * 	private String userName;	private String pwd;	public void setUserName(String userName) {		this.userName = userName;	}	public void setPwd(String pwd) {		this.pwd = pwd;	}	*/	private User user = new User();	public void setUser(User user) {		this.user = user;	}	public User getUser() {		return user;	}		public String login() {		// 获取用户名密码		System.out.println(user.getUserName());		System.out.println(user.getPwd());		// 把数据保存到域		ActionContext ac = ActionContext.getContext();		// 得到代表request的map		Map<String, Object> request = ac.getContextMap();		// 得到代表session的map		Map<String, Object> session = ac.getSession();		// 得到代表servletContext的map		Map<String, Object> application = ac.getApplication();				// 保存		request.put("request_data", "request_data");		session.put("session_data", "session_data");		application.put("application_data", "application_data");		return "login";		}}<action name="login" class="com.xp.b_action.UserAction" method="login">    		<result name="login">/index.jsp</result></action>d.共性问题问题1:Struts.xml配置文件没有提示解决a:找到struts-2.0.dtd文件,  拷贝到某个目录:d:/dtd /..  (不要用中文目录)让MyEclipse关联到上面dtd文件, windows preferences - 搜索xml catalog配置:Location:    上面配置的dtd目录Key:     -//Apache Software Foundation//DTD Struts Configuration 2.0//EN解决b:或者,让机器连接互联网,工具会自动下载dtd文件,缓存到MyEclipse中!7.配置详解
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"          "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>	<!-- 		package   定义一个包。 包作用,管理action。				   (通常,一个业务模板用一个包)		   name  包的名字; 包名不能重复;		   extends 当前包继承自哪个包		   		   在struts中,包一定要继承struts-default		   		  struts-default在struts-default.xml中定的包		   abstract  		   		  表示当前包为抽象包; 抽象包中不能有action的定义,否则运行时期报错		   		 abstract=true  只有当当前的包被其他包继承时候才用!		   		 如:		   		 	<package name="basePackage" extends="struts-default" abstract="true"></package>   					<package name="user" extends="basePackage">   		   namespace   名称空间,默认为"/"   		   				作为路径的一部分   		   			   访问路径=  http://localhost:8080/项目/名称空间/ActionName		action   配置请求路径与Action类的映射关系		  	   name  请求路径名称		  	   class 请求处理的aciton类的全名		  	   method 请求处理方法				result		  	   name  action处理方法返回值 		  	   type  跳转的结果类型		  	     标签体中指定跳转的页面		 	 -->    <package name="user" extends="struts-default" namespace="/">    	<action name="login" class="cn.itcast.b_execute.UserAction" method="login">    		<result name="login">/index.jsp</result>    	</action>    </package>     </struts>或者引入其他配置文件<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"          "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>	<!-- struts在运行时候会加载这个总配置文件: src/struts.xml -->    		<!-- 总配置文件中引入其他所有的配置文件 -->	<include file="com/xp/a_action/hello.xml"></include>	<include file="com/xp/b_execute/config.xml"></include></struts>


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表