Java framework -- Structs

Source: Internet
Author: User

Structs is one of the three major Java frameworks. Struts is an open source Framework that uses the assumervlet/JavaServer Pages technology to develop Web applications. Structs is an application framework based on MVC.

First, let's talk about MVC (Model/View/Controller ):
M is an exponential data model, which is usually represented by ActionForm Bean in Structs,
V refers to the user interface. A view is usually created by JSP. Structs contains the extended custom tag Library (TagLib), which can simplify the creation process of the user interface. Currently, tag libraries include Bean Tags, HTML tags, Logic Tags, Nested Tags, and Template Tags.
C is the Controller, in Structs to implement the control logic is Action, In the struts-config.xml configuration file ActionMapping and ActionForward specifies the different business logic or flow direction.
The purpose of using MVC is to separate the implementation code of M and V so that the same program can use different forms of representation. The purpose of C is to ensure the synchronization of M and V, once M changes, V should be updated synchronously.

Structs can only be used for Web application development. How does it work? In Struts, a user's request is generally set *. do is used as the request service name, all *. do requests are directed to ActionSevlet, which encapsulates user requests into a FormBean with a specified name based on the configuration information in the Struts-config.xml and transmits this FormBean to the ActionBean with the specified name, actionBean performs business operations, such as file operations and database operations. Each *. do has a corresponding FormBean name and ActionBean name, which are configured in the Struts-config.xml. So the core of Struts is ActionSevlet, the core of ActionSevlet is Struts-config.xml.

The following is a Demo on the login page to share with you:
The first is the JSP page code (where taglib is used ):
[Html]
<% @ Page language = "java" pageEncoding = "UTF-8" %>
 
<% @ Taglib uri = "http://struts.apache.org/tags-bean" prefix = "bean" %>
<% @ Taglib uri = "http://struts.apache.org/tags-html" prefix = "html" %>
<% @ Taglib uri = "http://struts.apache.org/tags-logic" prefix = "logic" %>
<% @ Taglib uri = "http://struts.apache.org/tags-tiles" prefix = "tiles" %>
 
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html: html lang = "true">
<Head>
<Html: base/>
<Title> logon page </title>
</Head>
 
<Body>
<Center>
<Font color = "red">
<Form action = "login. do" method = "post" focus = "login">
<Table border = "0">
<Tr>
<Td> User name: </td>
<Td> <input name = "username" type = "text"/> </td>
<Td>
<Font color = "red"> </Td>
</Tr>
<Tr>
<Td> password: </td>
<Td>
<Input name = "password" type = "password"/>
</Td>
<Td>
<Font color = "red"> </Td>
</Tr>
<Tr>
<Td colspan = "2" align = "center">
<Input type = "submit" value = "submit"/>
</Td>
</Tr>
</Table>
</Form>
</Center>
</Body>
</Html: html>


Followed by the ActionServlet code:

[Java]
Package com. sinosoft. servlet. action;
 
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
 
Import org. apache. struts. action. Action;
Import org. apache. struts. action. ActionForm;
Import org. apache. struts. action. ActionForward;
Import org. apache. struts. action. ActionMapping;
Import org. apache. struts. action. ActionMessage;
Import org. apache. struts. action. ActionMessages;
 
Import com. sinosoft. servlet. form. LoginForm;
 
Public class LoginAction extends Action {
 
Public ActionForward execute (ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
Throws Exception {
LoginForm loginForm = (LoginForm) form;
If (loginForm. getUsername (). equals ("xdp") & loginForm. getPassword (). equals ("123 ")){
// Login successful. Set the user information to the session
Request. getSession (). setAttribute ("user", loginForm. getUsername ());
// Jump to the specified page
Return mapping. findForward ("success ");
} Else {
// Login Failed, Process
ActionMessages errors = new ActionMessages ();
Errors. add ("loginerror", new ActionMessage ("login. error "));
This. addErrors (request, errors );
// Obtain the input attribute of the action in the configuration file and jump to this page
Return mapping. getInputForward ();
}
}
}


The code for ActionForm is as follows:
[Java]
Package com. sinosoft. servlet. form;
 
Import javax. servlet. http. HttpServletRequest;
Import org. apache. struts. action. ActionErrors;
Import org. apache. struts. action. ActionForm;
Import org. apache. struts. action. ActionMapping;
Import org. apache. struts. action. ActionMessage;
 
Public class LoginForm 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;
}

/**
* Verify the Function
*/
Public ActionErrors validate (ActionMapping mapping,
HttpServletRequest request ){
ActionErrors errors = new ActionErrors ();
// Determine the user name and add the error message
If (this. username = null | this. username. trim (). equals (""))
{
Errors. add ("username", new ActionMessage ("username. null "));
}
// Determine the password and add the error message www.heatpress123.net
If (this. password = null | this. password. trim (). equals (""))
{
Errors. add ("password", new ActionMessage ("password. null "));
}
Return errors;
}
}

We also added the configuration file ApplicationResources. properties to store the error message:


The last one is the struts-config.xml configuration information:
[Html] www.2cto.com
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE struts-config PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 1.2 //" http://struts.apache.org/dtds/struts-config_1_2.dtd ">
 
<Struts-config>
<Data-sources/>
<Form-beans>
<Form-bean name = "loginForm" type = "com. sinosoft. servlet. form. LoginForm"/>
</Form-beans>
<Global-exceptions/>
<Global-forwards/>
<Action-mappings>
<Action path = "/login" attribute = "loginForm"
Input = "/login. jsp" name = "loginForm" scope = "request"
Type = "com. sinosoft. servlet. action. LoginAction">
<Forward name = "success" path = "success. jsp"> </forward>
</Action>
</Action-mappings>
<Message-resources parameter = "com. yourcompany. struts. ApplicationResources"> </message-resources>
</Struts-config>

Related Article

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.