struts学习笔记

 

1、Struts框架总控制器actionserlet进行初始化struts-config.xml  读取配置文件
2、客户发送http请求  用户提交表单或是调用url想web应用程序提交请求
3、前端控制器根据请求填写formbean(Actionform) 填充form (实例化,复位,填充数据,校验,保存)
4、然后将请求转化到具体的Action执行exctue()方法处理。(可以通过代码进行实现)action业务功能控制器  派发请求也是根据actionconfig的信息
5、完成后台业务功能类完成商务调用 后面包括业务功能类javabean  调用其他业务逻辑模块,返回一个actionforward对象。
6、返回目标响应对象struts框架总控制器进行处理将后台处理结果的到 目标响应对象对应的一个具体的jsp页面或是action
7、转化HTTP请求道目标响应对象,最后出现在设图中。实现http响应。翻译响应,通常是一个具体的jsp页面。
<action path="/login" type="" name ="form's name">


PreparedStatement 是一个借口表示预编译的sql语句对象。里面有很多的方法
int rowCount = pstmt.executeUpdate();
Struts的mvc,是web层面的框架。
1.M .商业逻辑类,开发实现商业逻辑
2.V.view是由与控制器servlet配合的jsp标签库构成
3.C. 前端是serlet 将客户请求发送到后台等的控制器类action类

 

Action 什么时候初始化?
答:发送初始化请求的时候,不是在读取配置时初始化。
action初始化几次?在不同的浏览器上请求,只是初始化一次
struts1.1中action不是线程安全的。应为所有的请求共享一个action实例。
如何实现线程安全?
不用实例变量或类变量共享只是针对某个请求的数据
注意同步性

只能锁对象
   synchronized(count){
    
    count++;
   }

/***********************************************************************************************/
10、actionmapping 的研究分析
mapping 不能自己setpath、settype等。可以通过抛出异常来解决也可以不设置set访问器。去访问这个类
因为此类设置好了org.apache.struts.config.ActionConfig,中的很多的方法。
<forward name="addStudentFailure' path ="http://www.sohu.com" redirect="true" > redirect 默认是flase
redirect =false表示的是内部容器跳转,调用的方法是requestDispatcher.forward (路径为相对应用,请求转发)。.
..redirecct=true表示外部容器跳转,httpservletresponse.sendRedirect(容器外跳转容器,重新请求,写绝对路径)

<action-mappings>
    <action path="/login" type="com.LoginAction" name="loginForm" validate=“true‘>
     <forward name="loginsuccess" path="/LoginSuccess.jsp"></forward>
     <forward name="loginfailure" path="/LoginFailure.jsp"></forward>
    </action>
  </action-mappings>
/*****************************************************************************************/

11、ActionForm 的原理
*.do 请求--------------->查找action看action是否有name属性--------->根据name和scope scope=“request”。scope.getAttribute(name)----->是否找到 如果没有找到实例化一个(保存在特定的范围内),有就重用。
formbean中的reset()方法备用就是复位。------->从客户参数取值,request.getparamete()r调用对应的setter方法,队状态赋值,复位--------->如果validate的validate=true 进行校验成功---->
派发请求到action

scope 的缺省的值为request

先调用构造方法,再调用reset方法,最后调用set方法
jsp中的属性值name="sname" ,而在form中的属性不管是sname1还是什么 只要setSname是正确的也就是说只要有标准的set访问器

/*******************************************************************************************/

18、actionform相关的input属性表示如果验证错误直接跳到响应的url,如果name已经指定了就直接返回错误。

AcitonError error = new ActionErrors();
ActionMessage message = new ActionMessage("error");
error.add("error",message);


19、全局跳转
<global-forwards>
 <forward name="error" path="/error.jsp">
</global-forwards>
在struts-config.xml中进行配置。


21、clazz = class.forname().newinstance();
Field[] fields = agent.getClass().getDeclaredFields();
for(int i =0 ;i<fields.length;i++)
{
 String fieldName = fields[i].getName();
 Object obj = rs.getobject(fieldname);
 org.apache.commons.beanutils.beanutils.setProperty(clazz,fieldname,fieldValue);
}

 

22、模拟Struts填充formbean
java.util.Enumeration parameterEnumeration = request.getParameterNames();
Field[] fields = form.getClass().getDeclaredFields();
while(parameterEnumeration.hasMoreElements()){
 
 String parameterName= (String)parameterEnumeration.nextElement();
 for(Field field:fields)
 {
  String fieldName =field.getName();
  if(parameterName.equals(fieldName))
  {
   String value = request.getParameter(fieldName);
   try{
    org.apache.commons.beanutils.BeanUtils.setProperty(form,fieldName,balue)
   }catch(InvocationTargetException e)
   {
     e.printStackTrace();
   }
  }
 }
}

//用到了反射

23、struts标记库
jsp视窗组件所使用的struts标记库由四类标记组成:
bean标记:用来在jsp页中管理bean,struts-beans.tld
bean标记举例:
<%
1. String username ="wushu";
 request.setAttribute("wushu",username);

2. User user = new User();
 user.setUsername("wuxiaobin");
 request.setAttribut("user",user);
%>
  1.       <bean:define id="username1" name="username" scope="request" toScope="session">
  </bean:define>
  username1 ${sessionScope.username1} 
  或者使用 username1:<bean:write name ="username1'/>
 2.     真正的bean举例   <bean:define id ="username2" scope="request" name="user" property="username">
  username2:${username2}

<bean:write scope="request" name="user" property="username">

<bean:message

逻辑标记:用来在jsp页中控制流程,struts-logic.tld

html标记:用来生成html标记,在表单中显示数据,使用会话id对url进行编程struts-html.tld
tiels标记:使用动态模板构造普通格式的页struts-tiles.tld (框架标记)

 

25、bean_message ,包括标签的国际化
首先要在struts-config.xml
中配置message-resource
<message-resources parameter ="cn.itcast.application" key="mykey">

在jsp中e
<bean:message bundle="mykey" key="password" >
<input type="input" value="<bean:message bundle="mykey" key="password" >">
在jsp中引入标签的话是<%taglab uri="/web-inf/struts-bean.tld"(或是写成 http://struts.apache.org/tags-bean) prefix="bean"%>

 

26、逻辑标签
重复,条件,转发重定向
logic:iterate 的使用
1、name所指代的bean为集合类型

<logic:iterate id ="username" scope="request" name="usernames">
   ${username}
usernames 为request.setAttribute("usernames",usernames);

2、name+property
     name 不是结合,property必须是集合
<logic:iterate id="user" scope="request" name="userList" offse="2" indexId="i">
name 为request.setAttribut("userList",userList);
     ${i};
     ${user.username};
  <logic:iterate id ="favorite" name="user" propertity="favorittes">

  $ (favorite)
  </logic:iterate>

</logic:iterate>


27、动态表单form
actionform dynaactionform

<form-beans>
 <form-bean name="addStrudentForm" type="org.apache.strusts.action.DynaAction">
  <form-property name="sname" type="java.lang.String"></form-property>
  <form-property name="birth" type="java.sql.Date"></form-property>
  <form-property name="major" type="java.lang.String"></form-property>

</form-beans>

<action-mappings>
 <action path="/addStudentForm" type="cn.itcast.AddStudentaction" name="addStrudentForm">
 </action>


在java类的action的execute的方法中的代码是
DynActionForm addstuentForm =(DynaActionForm)form;
String sname = addStudentform.get("sname");
String major = (String)addStudent.get("major");

思考:如果界面中输入的sname是个数组应该怎么办呢?这时候用到了就是map
首先我们打算在action中进行forward
<forward: name="addStudnetSuccess" path="/web-inf/Addstudnetsuccess.jsp">
在这个页面中
${addStudentForm.map.sname} 实际上是getmap方法中取得的。

 


29、Struts的异常的处理
struts 中有两种一种是全局异常(global exception) 和局部异常(localexception)
在action标签中加入exception子标签 <exception >,且必须在forward前面。input =“放的是表单验证出错的跳转”
是action的excute的方法中出差错的情况下执行异常
<exception type="cn.itcast.exception" path="异常出错后所跳转的方向" key=“自定义名字,需要到资源文件中寻找值”>
  throw new ItcastException("key中自定义的名字,然后就会跳到path对应的jsp界面去")

message标签的功能可以实现对资源文件的注册。
<message-resources paramter ="cn.itcast.applicationresoutces"></message-resources>
然后出错的信息如果要在jsp中显示的话需要用到标签中的
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html'%>

 <html:errors/> //输出资源文件中key键值对所对应对的值。


30、插件的讲解
<plug-in className="org.apache.struts.tiles.TilesPlugin">
 <set-property property="definitions-config" value="/web-inf/tiles-defs.xml"
 >
</plug-in>
可以在中心控制器Actionservlet中的init方法对pulgin进行初始化,在destory中队平
插件销毁。
举例就是添加hibernate的插件。

31、ForwardAction
这个action的目的就是统一的形式访问相关的模块
(1)jsp在web-inf的目录下,不能直接访问。
(2)要求不能直接访问jsp,则直接访问action.do 的请求
这个可以作为pre 的访问页面去引导其他的jsp页面

32、DispachAction 的意思是派发行为
<action type=""  name="" path="" parameter="method">
 
调用的时候需要http://localhost:8080/myapp/saveSubscription.do?method=update

java 代码中的类的方法就是
public class StudentAction extends DispatchAction{

 public ActionForward add(ActionMapping mapping,ActionForm  form,HttpServletRequest request,HttpServletResponse response) throws Exception{
 }

 public ActionForward delet(ActionMapping mapping,ActionForm  form,HttpServletRequest request,HttpServletResponse response) throws Exception{
 }
}

<action path="/studentAction" type="cn.itcast.studentaction" name="addStudentForm" paramter="operatetype">

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值