struts可以运行在任何一个支持jsp1.2和servlet2.3的web container中struts将所有的请求提交到同一个中心控制器,org.apache.struts.action.actionservlet 类
web.xml配置
| <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"/> <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; } } |
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器