Struts1简介和入门攻略
什么是Struts1
Struts1是一个基于MVC设计模式的开源Web应用框架,可以快速构建基于Java EE的Web应用程序。它的主要组成部分包括Action、Form、Configuration、RequestProcessor等。
Struts1的优点
- 开源免费,社区支持活跃
- 遵循MVC设计模式,易于维护和扩展
- 可以快速开发Web应用,提高工作效率
Struts1的缺点
- 架构过于复杂,上手难度较高
- 不支持Ajax和RESTful等现代Web应用的开发方式
Struts1的入门步骤
- 下载和安装Struts1
- 官方网站:http://struts.apache.org/
- 下载最新稳定版压缩包,解压后即可使用
- 配置Struts1
- 在Web应用的WEB-INF目录下创建struts-config.xml文件
- 编写配置文件中所需要的组件:Action、Form、Handler、Forward等
- 编写Action和Form
- Action负责处理请求和响应,可以将用户的请求转发给相应的JSP页面或者其他的Action
- Form封装了用户提交的数据,可以在Action中直接操作
- 建立视图
- 在Web应用中建立对应的视图,展示所需信息
- 编写JSP页面
- 编写视图对应的JSP页面,接收Action返回的信息并展示
示例1
Action类
package com.example;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
public class HelloWorldAction extends org.apache.struts.action.Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) throws Exception {
String greeting = "Hello, world!";
request.setAttribute("message", greeting);
return mapping.findForward("success");
}
}
Form类
package com.example;
import org.apache.struts.action.ActionForm;
public class HelloWorldForm extends ActionForm {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
配置文件struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="helloWorldForm"
type="com.example.HelloWorldForm"/>
</form-beans>
<action-mappings>
<action path="/helloWorldAction"
type="com.example.HelloWorldAction"
name="helloWorldForm">
<forward name="success" path="/success.jsp"/>
</action>
</action-mappings>
</struts-config>
对应的JSP页面success.jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello, world!</title>
</head>
<body>
<h1><%= request.getAttribute("message") %></h1>
</body>
</html>
示例2
Action类
package com.example;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class CalculatorAction extends org.apache.struts.action.Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) throws Exception {
CalculatorForm calculatorForm = (CalculatorForm)form;
int result = 0;
if (calculatorForm.getOperation().equals("+")) {
result = calculatorForm.getNumber1() + calculatorForm.getNumber2();
} else if (calculatorForm.getOperation().equals("-")) {
result = calculatorForm.getNumber1() - calculatorForm.getNumber2();
} else if (calculatorForm.getOperation().equals("*")) {
result = calculatorForm.getNumber1() * calculatorForm.getNumber2();
} else if (calculatorForm.getOperation().equals("/")) {
result = calculatorForm.getNumber1() / calculatorForm.getNumber2();
} else {
request.setAttribute("message", "Unsupported operation: " + calculatorForm.getOperation());
return mapping.findForward("error");
}
request.setAttribute("result", result);
return mapping.findForward("success");
}
}
Form类
package com.example;
import org.apache.struts.action.ActionForm;
public class CalculatorForm extends ActionForm {
private int number1;
private int number2;
private String operation;
public void setNumber1(int number1) {
this.number1 = number1;
}
public int getNumber1() {
return this.number1;
}
public void setNumber2(int number2) {
this.number2 = number2;
}
public int getNumber2() {
return this.number2;
}
public void setOperation(String operation) {
this.operation = operation;
}
public String getOperation() {
return this.operation;
}
}
配置文件struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="calculatorForm"
type="com.example.CalculatorForm"/>
</form-beans>
<action-mappings>
<action path="/calculatorAction"
type="com.example.CalculatorAction"
name="calculatorForm"
parameter="operation">
<forward name="success" path="/success.jsp"/>
<forward name="error" path="/error.jsp"/>
</action>
</action-mappings>
</struts-config>
对应的JSP页面calculator.jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Calculator</title>
</head>
<body>
<form method="post" action="calculatorAction.do">
<input type="text" name="number1" /><br />
<select name="operation">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select><br />
<input type="text" name="number2" /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
对应的JSP页面success.jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Calculator</title>
</head>
<body>
<h1>Result: <%= request.getAttribute("result") %></h1>
</body>
</html>
对应的JSP页面error.jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Error</title>
</head>
<body>
<h1><%= request.getAttribute("message") %></h1>
</body>
</html>
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Struts1简介和入门_动力节点Java学院整理 - Python技术站