Struts1 the next day

Source: Internet
Author: User

The basic writing process for struts:
1 specified in Web.xml
*.do ===== Org.apache.struts.action.ActionServlet
2 requiring clients to send requests must follow the *.do approach
3 Web processing class extends action{
Execute (actionmapping,actionform,httpservletrequest,httpservletresponse) {
Get the parameters in the client page
Req.getparameter ("uname");
MyForm f = (MyForm) Form;
F.getuname ();
Methods for calling the business layer
biz.** ();
Determines the target resource for a jump based on the return result
Return Mapping.findforward ("logical name");
}
}
[4 Writing Formbean]
* * Extends actionform{
Provides a private property with a property name that must be the same as the name of the control on the page
Provides a total of set/get for the above properties
}
5 Configuration Struts-config.xml
<form-bean name= "Formbean logical Name" type= "Formbean classpath" >

<action path= "client-sent request Name=" Formbean logical Name "Type=" Class path of Action ">

Path accepts information to find out if the Name property exists, and then populates each of the Formbean property values with the individual control values in form forms in the page. Then find the action information for the type attribute.

The function and writing way of Forwardaction:
Function: page jump page, in order to conform to the core of MVC control idea
Page-----forwardaction----Page
Write:
1 page:
<a href= "Page.do" >
2 Struts-config.xml.
<action path= "/page" type= "Org.apache.struts.actions.ForwardAction"
Parameter= "Real resource Path" >

Number of Actionform in the project:
0
1
N

add.jsp----->
Allform-----> Testallformaction
testallform.jsp----->

Actionform of the page and the corresponding relationship:
Any page----0 form:
Get the parameter in action, can only crawl manually: Req.getprameter ();
One page---1 form
Get the parameters in the action, you can crawl it manually, or you can get it through the form
Multiple pages---1 form
Get the parameters in the action, you can crawl it manually, or you can get it through the form

Struts1 's flaws:
1 Struts1 tags are directly coupled to the Actionform
2 Web projects based on the STRUTS1 framework and strong coupling of the STRUTS1 API

Attention:
Relevant parameter information in the history page is saved in Actionform
To prevent data collisions, you need to override the parent class Actionform method in Formbean.
In the Reset method, set all the property values in the current Formbean to the default values.
Such as:
@Override
public void Reset (actionmapping mapping, HttpServletRequest req) {
This.uname = null;
This.birthday = null;
This.stuno = null;
this.var1 = 0;
THIS.VAR2 = null;
}

Check:
1 Client Checksum JavaScript

Server-side checksum actionform biz Layer

2 format Checker JavaScript Actionform

Business Check Biz Layer

The role of Actionform:
1 automatically accepts data entered by the client, and automatic type conversion
2 server-side formatted checksum for client-submitted data

Actionform The writing process of the server-side formatted checksum:
1 overrides the Validate method in the Actionform parent class, officers transferred Guevara the rule is written in the method
Public actionerrors Validate (actionmapping mapping, HttpServletRequest req) {
Actionerrors errors = new Actionerrors ();
if (this.getuname () = = "") {
Actionmessage error = new Actionmessage ("Error.uname.empty");
Errors.add ("", error);
}
if (This.getuname (). Length () > 2) {
Actionmessage error = new Actionmessage ("Error.uname.length");
Errors.add ("", error);
}
return errors;
}

2 Write the resource file messagereourse.properties, fill in the error description information
error.uname.empty=/u59d3/u540d/u4e0d/u80fd/u4e3a/u7a7a
Error.uname.length=/u59d3/u540d/u7684/u957f/u5ea6/u5fc5/u987b/u5c0f/u4e8e2

3 Path to the configuration resource file
<message-resources parameter= "Messageresourses" ></message-resources>

4 <action path= "Request Information" name= "Formbean logical Name"
Validate= "true" input= "the resource path for validation failed jumps" type= "Classpath for Action" >

5 Display error description information on the page
<% @taglib prefix= "html" uri= "http://struts.apache.org/tags-html"%>

Multiple business processes correspond to the same action:
All.do---->allaction

add.jsp---All.do?name=add
delete.jsp---All.do?name=delete

Allaction extends action{
Execute (MAPPING,FORM,REQ,RESP) {
String methodname = req.getparameter ("name");
if (Methodname.equals ("add")) {
Add (MAPPING,FORM,REQ,RESP)
}
if (methodname.equals ("delete")) {
Delete (MAPPING,FORM,REQ,RESP)
}

}
Public Add (MAPPING,FORM,REQ,RESP) {
.. add
}
Public Delete (MAPPING,FORM,REQ,RESP) {
.. delete
}
}

>stumgmtaction--addstu method of add.jsp-----/add.do?methodname=add--------------
Index.jsp---/findallstus.do?methodname=findall-->stumgmtaction--findall method

Org.apache.struts.actions.DispatchAction
Distribution Processing class, features:
  can organize a set of related business operations into a class for easy maintenance and management
writing processes:
 1 Web processing Class
 stumgmtaction extends dispatchaction{
   cannot overwrite Execute method
   You must provide more than one business method, the same as the Execute method signature, which is custom
  public actionforward findall (actionmapping mapping, Actionform form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
  istumgmtbiz biz = new Stumgmtbi Zimpl ();
  Set<Student> stus = null;
  try{
   stus = biz.showallstudents ();
  }catch (Exception e) {
   e.printstacktrace ();
   return Mapping.findforward ("ErrorPage");
  }
  req.setattribute ("Stus", Stus);
  return Mapping.findforward ("Showstuspage");
  }
  ..
 }

2 The page needs to specify a special parameter that is to find the Stumgmtaction specific processing method
1) Operate.do?methodname=addstu
2) <input type= "hidden" name= "methodname" value= "Addstu" >
3 configuration file, all requests need to be configured only to the same processing class
<action path= "/operate" type= "com.sinojava.stuMgmt.web.dispathAction.StuMgmtAction" parameter= "methodname" >
<forward name= "Showstuspage" path= "/web-inf/page/showall.jsp" ></forward>
<forward name= "ErrorPage" path= "/web-inf/page/error.jsp" ></forward>
<forward name= "Indexpage" path= "/index.jsp" ></forward>
</action>
============================================
The value of the paramter parameter <==> the name of the special parameter specified in the page
============================================

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.