- 在struts官网下载struts-1.3.10-all.zip https://struts.apache.org/download.cgi
- 在Eclipse创建WEB工程,并将struts-1.3.10lib目录的jar拷贝到WEB工程的lib目录
3、参考struts-1.3.10srcappsexamples的例子,在WEB-INF目录下创建web.xml
Struts Examples Application action org.apache.struts.action.ActionServlet config /WEB-INF/struts-config.xml 2 action *.do index.jsp index.html upload.jsp
4、参考struts-1.3.10srcappsexamples的例子,在WEB-INF目录下创建struts-config.xml
5、创建ActionForm:examples. CustomFormBean.java
package examples;
import java.io.Serializable;
import org.apache.struts.action.ActionForm;
public class CustomFormBean extends ActionForm implements Serializable {
// --------------------------------------------------- Instance Variables
private String example = "Hello World 您好";
// ----------------------------------------------------------- Properties
public String getExample() {
return (this.example);
}
public void setExample(String example) {
this.example = example;
}
}
6、创建Action:examples.CustomAction.java
package examples;
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 CustomAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
// Extract attributes we will need
CustomFormBean info = (CustomFormBean) form;
ActionForward forward = mapping.findForward("success");
System.out.println("##########forward##########" + forward.getPath());
return mapping.findForward("success");
}
}
7、在WEB应用程序的根目录下创建index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ page isELIgnored="false" %>
使用struts标签:
使用EL表达式:
8、通过eclpipse将WEB工程打包成strut1.war,发布到tomcat
9、启动tomcat,在浏览器输入地址http://127.0.0.1:8080/struts1/welcome.do
编程环境:jdk1.8、Eclipse2020、tomcat8.5、Struts1.3
备注:代码阅读或调试过程遇到问题或有任何疑问,欢迎留言,谢谢阅读
Struts的故事
Struts是全世界最早的MVC框架,其作者是JSP规范的制定者,并参与了Tomcat的开发,这个名字来源于在建筑和滑翔机中用来支持的金属架 ,猜测着 Struts 的开发者大概是希望用该框架来支持 JSP 、 Servlet 、 Java 这些技术在 Web 项目中的使用。
Struts 的第一个版本在 apache网站上发布的时候。它只提供了一种分离视图和业务应用逻辑的 Web 应用方案。因为在 Struts 之前,开发人员都是在 JSP 里写入处理业务逻辑的 Java代码,尤其是涉及到数据库和页面 Form 表单数据之间交互的时候,开发人员在每个页面都要写入像连接数据库这样的 Java代码,导致了大量的代码冗余。



