当前页面: 开发资料首页 → Struts 专题 → Struts开发指南之安装与使用
摘要: Struts开发指南之安装与使用 Struts可以运行在任何一个支持JSP1.2和Servlet2.3的WEB Container中Struts将所有的请求提交到同一个中心控制器,org.apache...
<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> <load-on-startup>2</load-on-startup> <servlet-mapping> |
<servlet-name>action</servlet-name> <url-pattern>*.do或/do/*</url-pattern> </servlet-mapping> |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config> <data-sources /> // 定义数据源 <form-beans /> // 定义ActionForm <global-exceptions /> // 定义全局异常 <global-forwards /> // 定义全局转向url <action-mappings /> // 定义action <controller /> // 配置Controller <message-resources /> // 配置资源文件 </struts-config> |
<form method="post" action="/example.do"> 请输入值 <input type="text" name="test"/> <br><br> <input type="submit" name="Submit" > <input name="reset" type="reset" > </form> |
<struts-config> // 配置formbean <form-beans> <form-bean name="exampleActionForm" type="com.zotn.struts.example1.ExampleActionForm" /> </form-beans> // 配置action <action-mappings> <action name="exampleActionForm" path="/example" type="com.zotn.struts.example1.ExampleAction"> // action内部的foward <forward name="foward" path="/foward.jsp" /> </action> </action-mappings> </struts-config> |
public class ExampleAction extends Action { public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) { // 得到对应的form ExampleActionForm eaf = (ExampleActionForm)actionForm; // 取得输入的test String test = eaf.getTest(); // 判断并将值放入request if ("".equals(test)){ request.setAttribute("test","empty"); }else{ request.setAttribute("test",test); } // 通过mapping寻找相应的url,返回ActionFoward return actionMapping.findForward("foward"); } } |
public class ExampleActionForm extends ActionForm { private String test; public String getTest() { return test; } public void setTest(String test) { this.test = test; } } |