1.什么是Struts1
基于MVC的轻量级,WEB应用框架
2.struts1工作原理
a> 初始化: struts框架的总控制器ActionServlet是一个Servlet,它在web.xml中配置成自动启动的Servlet,在启动时总控制器会读取配置文件(struts-config.xml)的配置信息,为struts中不同的模块初始化相应的对象.
b> 发送请求: 用户提交表单或通过URL向WEB服务器提供请求,请求的数据用HTTP协议传给WEB服务器.
c> form填充: Struts的总控制器ActionServlet在用户提交请求时将数据放到对应的form对象中的成员变量中
d> 派发请求: 控制器根据配置信息对象ActionConfig将请求派发到具体的Action,对应的formBean一并传给Action的excute()方法
e> 处理业务: Action一般只包含一个excute()方法,它负责执行相应的业务逻辑(调用其它的业务模块)完毕后返回一个ActionForward对象.服务器通过ActionForward对象进行转发工作
f> 返回响应: Action将业务处理的不同结果返回一个目标相应对象给总控制器
g> 查找响应: 总控制器根据Action处理业务返回的目标响应对象,找到对应的资源对象,一般情况下为jsp页面
h> 响应用户: 目标响应对象将结果传递给资源对象,将结果展现给用户
3.开发指南
准备工作:
1> 到http://jakarta.apache.org/下载struts的安装文件struts-1.3.10-all.zip
2> 解压struts-1.3.10-all.zip
3> 创建一个 web project,并将上步中解压的lib文件下的jar包引进去
4> 配置web文件
<servlet>
<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>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
5> 新建struts-config.xml
<struts-config>
<form-beans></form-beans>
<global-forwards></global-forwards>
<action-mappings></action-mappings>
</struts-config>
6> 创建数据库(mysql)
4. 项目开发
4.1 页面制作 index.jsp
<body>
<center>titles</center>
<hr/>
<li><a href = "student.do?action=query">students's management system</a></li>
</body>
4.2 在struts-config.xml中需要修改
<struts-config>
<form-beans>
<form-bean name = "studentForm" type = "com.StudentForm"/>
</form-beans>
<global-forwards></global-forwards>
<action-mappings>
<action path="/student" name = "studentForm" scope = "request" type = "com.StudentForm"/>
</action-mappings>
</struts-config>
4.3 创建com.javakc.struts1.action.StudentAction 业务类
public class StudentAction extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return queryStudent(mapping,form,request,response,action);
}
4.4 创建com.javakc.struts1.action.StudentForm类
1)继承org.apache.struts.action.ActionForm类
2)属性名要和表单中的属性名一一对应,并生成其对应的getter、setter方法
package com;
import org.apache.struts.action.ActionForm;
public class StudentForm extends ActionForm{
private String id;
private String name;
private String sex;
private String age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
4.5 在StudentAction中处理提交的请求
1) 通过form表单或超链接可以将用户的请求提交到服务器进行处理,在系统架构中,服务器处理用户请求的场地就是StudentAction类的execute方法.
public class StudentAction extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String action = request.getParameter("action");
if(action == null||"".equals(action)){
return mapping.findForward("error");
}else if("query".equals(action)){
return queryStudent(mapping,form,request,response,action);
}
// TODO Auto-generated method stub
return mapping.findForward("error");
}
public ActionForward queryStudent(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response,String action){
StudentForm studentForm = new StudentForm();
studentForm.setId("1");
studentForm.setName("LI");
studentForm.setSex("M");
studentForm.setAge("10");
request.setAttribute("studentForm",studentForm);
return mapping.findForward(action);
}
}
4.6 在student_info.jsp中展示数据
<body>
<div align = "center">
<font color = "red" face = "黑体" size ="+2">上海交通大学</font>
<table width = "60%">
<tr>
<div align= "right"><a href = "add.jsp">增加学生信息</a></div>
</tr>
</table>
<table algin= "center" width = "60%" cellpadding = "2" border = "1" bordercolor = "green">
<tr>
<td>序号</td>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>修改</td>
<td>删除</td>
</tr>
<tr height = "25">
<td>${studentForm.id}</td>
<td>${studentForm.name}</td>
<td>${studentForm.sex}</td>
<td>${studentForm.age}</td>
<td align = "center"><a href = "#">修改</td>
<td align = "center"><a href = "#">删除</td>
</tr>
</table>
</div>
</body>