Struts1.3——Struts入门

1.Struts的几个基本概念

1.struts是一个开源框架(frameset) 
2.struts是一个Web框架 
3.struts是一个基于MVC的Web框架html

2.为何有struts

由于咱们对MVC的理解不一样,可能形成不一样公司写程序的时候,规范不统一,这样不利于程序的维护和扩展以及提升开发效率,因此咱们有必要用一个统一的规范来开发项目。因此出现了struts.java

struts是经过采用Java的Servlet/JSP技术,实现了基于Java EE Web应用的Model-View-Controller(MVC)设计模式的应用框架,是MVC经典设计模式中的一个经典产品程序员

struts的优缺点以下: 
1.struts的好处:web

程序更加规范化 
程序开发的效率提升了 
程序的可读性增长 
程序的可维护性增长apache

2.struts的不足之处:设计模式

form表单有点鸡肋 
action是单态(对网站并发性的处理有影响)安全

这些内容后面会介绍。并发

咱们要知道的是:框架在提升了程序的规范的同时,也约束了程序员的自由。app

3.struts的原理

以一个用户登陆验证的例子说明。框架

3.1.时序图

时序图

1.ActionServlet是总控制器,它是struts给咱们提供的,咱们无需本身写,只须要配置便可。ActionServlet管理struts-config.xml文件,而这个文件是struts的核心文件,该文件配置了ActionForm,还配置了Action,以及它们的对应关系等。 
2.ActionForm是表单,用于存放数据。开发ActionFrom必需要继承ActionForm类,这是规范。 
3.Action是分控制器,在Struts中,Action能够有多个.它的本质就是Servlet。开发一个Action也要继承Action类。 
4.Model(Java类,Service类,ejb等)

3.2.struts入门案例

以上述用户登陆验证来演示,而且使用手动配置的方式来开发第一个struts项目,项目名称为strutslogin。 
首先,项目目录结构以下所示:

项目目录结构

【步骤】: 
1.在开发struts的时候,须要struts的开发包

struts的开发包可到官网下载【下载地址】,它的最新版本是2.5.1(2016年6月21日)。因为目前所学为1.3的版本,因此我下载Struts 1.3.10。并把全部的jar包加入到当前项目的lib文件夹。


2.先写出login.jsp

写最简单的登陆页面login.jsp,以下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <form action="/strutslogin/login.do" method="post"> UserName:<input type="text" name="username"/><br><br> Password:<input type="password" name="password"/><br><br> <input type="submit" value="Login"/> </form> </body> </html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 注意,为了安全起见,login.jsp放在WEB-INF目录下,在WEB-INF目录外经过index.jsp转发到login.jsp。
  • 注意form中的action跳转地址,是咱们的web应用下的login.do页面,这里在稍后介绍的struts-config.xml配置中会再说明。 

3.编写ActionForm和Action

(1)首先须要编写ActionForm,即用户登陆所用的表单,用于填充用户输入的数据。咱们取名为UserForm,它继承自ActionForm类,以下:

public class UserForm extends ActionForm { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

其实这就是一个很简单的JavaBean,它和login.jsp中的表单对应,咱们在这个UserForm中定义属性usernamepassword,并生成它们的settergetter方法。 
这里须要注意的是:

<1>.通常的规范:定义属性名称的时候应该和jsp页面的控件名称同样。 
<2>.但其实,属性的名称并不是必定要一致,而真正要保持一致的是settergetter方法。也就是说,若是你在jsp中定义的控件名称为username和password,那么setter方法的名称必定是setUsernamesetPassword,同理,getter方法的名称必定是getUsernamegetPassword。属性能够换其余的名字,可是这几个方法的名称必定要固定格式:get/set+控件名称而且首字母大写 
<3>.但咱们遵照通常的规范最好。 
<4>.ActionForm必定要按照上述规范写好,才能保证数据可以正确填充,才能保证work

(2)而后,编写Action,即登陆的Action,咱们取名为LoginAction,它必须继承自Action类,以下:

public class LoginAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // 把form转成对应的UserForm对象 UserForm userForm = (UserForm)form; System.out.println("用户名="+userForm.getUsername()); System.out.println("密码="+userForm.getPassword()); // 简单验证 if("123".equals(userForm.getPassword())){ // 若是用户密码为123,则为合法 return mapping.findForward("ok"); }else{ return mapping.findForward("error"); } } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

在这个类中,咱们须要重写一个方法:excute。这个方法会处理业务逻辑,会自动调用,它有点相似于Servlet中的service方法,或者doGet/doPost方法。


4.配置struts-config.xml文件,这个文件通常放在/WEB-INF目录下,它配置Action、ActionForm以及它们的对应关系和跳转

<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Structs Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <!-- 配置表单 --> <form-beans> <!-- name是表单名字,能够随意写,可是建议取名规范:表单类名小写 --> <!-- type用于指定表单类的全路径 --> <form-bean name="userForm" type="com.gavin.forms.UserForm"></form-bean> </form-beans> <!-- 配置Action --> <action-mappings> <!-- 配置一个具体的action --> <!-- path表示未来访问该action的资源名,http://localhost:8080/web/path --> <!-- name用于关联某个表单 --> <!-- type用于指定该Action类的全路径 --> <action path="/login" name="userForm" type="com.gavin.actions.LoginAction"> <!-- 这里配置跳转关系 --> <!-- name表示结果名称,path表示转发到的页面地址 --> <forward name="ok" path="/WEB-INF/welcome.jsp"/> <forward name="error" path="/WEB-INF/error.jsp"/> </action> </action-mappings> </struts-config>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

(1)这个struts-config.xml引入了一个DTD文件:struts-config_1_3.dtd,这个文件能够在咱们下载的jar包中找到,它定义了咱们这个xml的约束,根元素为struts-config。 
(2)该配置文件主要经过form-beans来配置ActionForm、经过action-mappings来配置Action。 
(3)咱们在form-beans下用form-bean来配置具体的ActionForm,好比当前文件配置了咱们写的UserForm 
(4)在action-mappings下用action来配置具体的Action,好比当前文件配置了咱们写的LoginAction 
(5)要注意这里action中的path参数配置,它就是咱们的login.jsp中的表单action提交的地址,login.jsp中写的是login.do,这个login.do会交给struts的总控制器ActionServlet来处理,最后ActionServlet将经过必定的机制找到这里的path参数配置为/login的action,再经过该action配置的type参数找到具体的类。

从这个过程当中咱们能够发现,login.do在经过ActionServlet的处理后,最后找到的倒是路径为/login的action,这彷佛和.do这个后缀没有什么关系,的确是这样的,这个后缀只是习惯上的用法,咱们彻底能够在web.xml文件中将这个后缀设置成其余任何的名字。 
(6)最后要在具体的action下配置跳转关系。这里配置的参数和LoginAction中的excute方法所写要对应起来。


5.写出welcome.jsp和error.jsp页面

写出两个简单的界面便可。


6.在web.xml中配置ActionServlet

struts的总控制器ActionServlet本质上仍是一个Servlet,因此咱们要在web.xml中配置,配置以下:

<servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <!-- 指定配置文件struts-config.xml的路径 --> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

咱们要在该配置中指定struts-config.xml文件的地址。同时能够看到,咱们在这里配置的后缀为*.do,也就是说全部以.do结尾的请求都要交给ActionServlet来处理。到这里,也就明白了为何咱们的login.jsp中写的提交地址为login.do

4.总结

经过手工完成一个简单的struts项目,可让咱们对struts的工做流程和工做原理有更加深入的认识。这个过程略微繁琐,后面会有工具直接生成,可是掌握底层原理是咱们学习必不可少的一个步骤。

总结一下,其实struts是对咱们经过JSP/Servlet和使用MVC模式开发项目的一个更高层次地提炼,它帮咱们作了大部分工做,咱们只须要按照struts框架提供的流程填充便可,简化了咱们的开发的难度。

struts中的核心知识点:ActionServlet总控制器,Action分控制器,ActionForm表单,以及最重要的structs-config.xml和web.xml文件的配置。

相关文章
相关标签/搜索