使用struts1.x就得导入相对应的jar包;
antlr.jar
commons-beanutils.jar
commons-digester.jar
commons-fileupload.jar
commons-logging.jar
commons-validator.jar
jakarta-oro.jar
struts.jar
需要更改 java Web 项目里的 web.xml ;在下载 struts 的 jar 包同时会有实例程序 *.war 文件;用压缩程序打开;找到实例程序中的 web.xml ,可以直接 copy:
<servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>2</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <!-- Standard Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
具体的可以 Google ;
然后将实例中的程序的struts-config.xml文件copy到WEB-INF下:将<struts-config></struts-config>中的代码和注释全部删除;这样一个struts1.x的环境就搭建好了;
由简单的计算器例子带我们认识下struts1.x:
ActionForm:
package com.keith; import org.apache.struts.action.ActionForm; public class CountForm extends ActionForm { private int first; private int second; private String operator; public int getFirst() { return first; } public void setFirst(int first) { this.first = first; } public int getSecond() { return second; } public void setSecond(int second) { this.second = second; } public String getOperator() { return operator; } public void setOperator(String operator) { this.operator = operator; } }
Count:
package com.keith; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public class CountAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { CountForm cf = (CountForm) form; int first = cf.getFirst(); int second = cf.getSecond(); String operator = cf.getOperator(); int result = 0; try { if ("+".equals(operator)) { result = first + second; } else if ("-".equals(operator)) { result = first - second; } else if ("*".equals(operator)) { result = first * second; } else if ("/".equals(operator)) { result = first / second; } request.setAttribute("result", result); return mapping.findForward("suc"); } catch (Exception e) { e.printStackTrace(); return mapping.findForward("ero"); } } }
index.jsp:
<form action="count.do" method="post"> <input type="text" name="first"> <select name="operator"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> <input type="text" name="second"> <input type="submit" value="="> </form>
result.jsp:
计算结果为:<% CountForm cf1 = (CountForm)request.getAttribute("CountForm"); %> <%= cf1.getFirst() %> <%= cf1.getOperator() %> <%= cf1.getSecond() %>= <%=request.getAttribute("result") %>
看下配置文件struts-config.xml:
<struts-config> <form-beans> <form-bean name="CountForm" type="com.keith.CountForm" /><!-- 给ActionForm取个名字,并指出它的位置--> </form-beans> <action-mappings> <action path="/count" type="com.keith.Count" name="CountForm" scope="request"> <!-- path:提交的时候给出的请求名; type:提交给哪个action执行 name:指出action用到的ActionForm 的名字,由于xml是key-value的,所以知道key,就知道value; scope:指范围,一般用requese;--> <forward name="suc" path="/resulet.jsp" /> <!-- 给mapping.findForward:的名,并指定跳转的页面--> <forward name="ero" path="/ero.jsp" /> </action> </action-mappings> </struts-config>
以上是初步的认识struts1.x下一篇将系统的介绍struts1.x;虽然它已过时,但是经典,适于学习;