struts入门例子
struts例子
1.找到struts相关的jar文件,拷贝到WEB-INF\LIB目录中,应该是八个jar文件: antlr.jar, commons-beanutils.jar, commons-digester.jar, commons-fileupload.jar, commons-logging.jar, commons-validator.jar, jakarta-oro.jar, struts.jar 2.导入到项目tomcat2中,在tomcat2项目中右键选Properties->Java Build path->Libraries->Add External JARS...,全选WEB-INF\LIB中的八个jar文件. 3.创建HelloAction.java文件.在WEB-INF/src中选择test包,然后右键创建一个新类。 name:HelloAction Superclass:Browse->输入action->选择Action-org.apache.struts.action 4.打开HelloAction.java后,右键选source->Override/Implement Methods->execute 这样我们就重载/实现了父类的execute方法.因为父类的execute有二个同名方法,选择参数为Http开头的. 将: public ActionForward execute(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception { // TODO Auto-generated method stub return super.execute(arg0, arg1, arg2, arg3); } 修改: public ActionForward execute(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception { // TODO Auto-generated method stub return arg0.findForward("success"); } 5.在WEB-INF目录上右键创建一个web.xml,这里利用Myeclipse插件带的xml项目来创建. MyEclipse->XML->XML(Advanced Templates) fileName:web.xml Template to use:XML template for a web.xml file with Struts 保存时会提示已经有一个web.xml了(上个实例创建的),这里覆盖它. 打开web.xml这时已经为建立了模板,经过修改:
6.在WEB-INF目录上右键创建一个struts-config.xml,这里利用Myeclipse插件带的xml项目来创建. MyEclipse->XML->XML(Advanced Templates) fileName:struts-config.xml Template to use:XML template for a struts-config.xml file 打开struts-config.xml经过修改:
7.在tomcat2上右键新建一个jsp文件.filename:hello.jsp 源码: <%@ page contentType="text/html; charset=gb2312" %>
8.将tomcat服务停止一下,然后打开. 输入http://localhost/hiAction.do 这时可以看到打开了hello.jsp文件. **注意:不管是java代码还是xml都是区分大小写的。 **