Struts简介(个人见解)
关键字: Struts1.网址:http://struts.apache.org/
2.版本:
-
1.3.8: 本次简介的版本, 当前最新版本1.3.9 BETA
- 2.0.9: 和1.x版本不兼容
3.Struts的基本特点
对Servlet和JSP的增强, 减少代码编写量
基于Web MVC模式,规范java Web开发
改善java Web开发的扩展性
4.Struts的优缺点
优点
- 使用广泛, 用户众多, 就业有优势;
- 历史较长, 有成熟的第三方工具支持;
- 有很多web应用使用struts.
缺点
- 由于历史较长, 一些功能比较落后;
- 配置较为繁琐;
- 编程模型需要线程安全的支持, 影响性能.
5.编写一个最简单的struts应用
(1)在Eclipse环境下创建新的web项目
(2)配置Web.xml文件加入如下内容:
action
org.apache.struts.action.ActionServlet
config
>/WEB-INF/struts-config.xml
0
action
*.do(3)编写一个空的struts.config.xml文件
version= "1.0" encoding= "UTF-8"?>
struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
下面可以先写一个简单的页面表单认证的JSP页面,并写好FormBean和实体Bean,同时编写一个假的登录成功JSP页面,将登录时输入的信息显示在页面。另FormBean和实体Bean的成员名称应当一直,便于Struts在复制成员时正确的将FormBean中的内容复制于实体Bean中,FormBean中成员的类型一般均为String类型,Struts在复制时会做自动转型,但是要注意Struts在转型中无法正确转型java.util.Date,只能转型为java.sql.Date,所以我们需要在Action中做手动转型,以后我会专门介绍。
6.编写步骤:
第一步:编写登录用jsp页面
<?xml version= "1.0" encoding= "UTF-8" ?>
<%@ page language= "java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd >
<html xmlns=http://www.w3.org/1999/xhtml >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>登录</title>
</head>
<body>
<form action= "logon.do" method= "post">用户名: <input type="text"
name="userName" /><br />
密码:<input type="password" name= "password" />
<input type="submit" name="Submit" value="登录" /></form>
</body>
</html>第二步:编写登录成功页面
<?xml version= "1.0" encoding= "UTF-8" ?>
<%@ page language= "java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd >
<html xmlns=http://www.w3.org/1999/xhtml >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>登录成功</title>
</head> <body> ${param.userName }登录成功. </body> </html>第三步: 编写FormBean
package struts.logon; import org.apache.struts.action.ActionForm; public class LogonFormBean extends ActionForm { /** * */ private static final long serialVersionUID = 1L; private String userName; private String password; //省略getter和setter }
第四步: struts-config.xml中配置FormBean
...
name="logonForm" type="struts.logon.LogonFormBean" />
...第五步: 在Action中编写控制类的代码
用于实现页面的跳转控制
继承org.apache.struts.action.Action
覆盖execute方法
@Override
public ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) throws Exception {
LogonFormBean bean = (LogonFormBean) actionForm;
if (bean.getUserName().equals( "test" )) {
return actionMapping.findForward( "success" );
}
return actionMapping.findForward( "error" );
}
}方法的逻辑: 如果用户名等于"test", 跳转到成功, 否则返回登录
第六步: 配置控制功能
控制功能负责页面的跳转
如果用户名等于"test", 跳转到成功页面, 否则返回登录页面
在struts-config.xml文件中加入:
path="/logon" type="struts.logon.LogonAction" name="logonForm" >
name以上只是我对Struts的基本的介绍,并且做了一个最简单的helloworld程序,只是相和朋友们一起学习,有不对或不足的地方希望打家帮我指出
="success" path="/success.jsp" />
name="error" path="/logon.jsp" />