Struts integrates ExtJS and Some Problems Encountered (ModelDriven)

Source: Internet
Author: User
Tags i18n
1 Preparation: in addition to the usual introduction of struts2 jar package, but also need to introduce struts2-json-plugin-2181jar; json-lib-21jar of these two packages. 2. Create our model: Userpackageedutstcmodel; publicclassUser {privatei

1. preparations:

In addition to the usual introduction of struts2 jar package, but also need to introduce struts2-json-plugin-2.1.8.1.jar; json-lib-2.1.jar these two packages.

2. Create our model: User

Packageedu. tstc. model;

 

Public classUser {

Private int id;

Private String username;

Private String password;

Public int getId (){

Return id;

}

Public void setId (int id ){

This. id = id;

}

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;

}

}

 

 

3. Create our Action

Here is a simple demonstration of the Integration of struts-2 and extjs, so all the business logic is processed in the Action. Create our LoginAction. When the form is submitted, we can get the data in LoginAction.

Packageedu. tstc. action;

 

Import edu. tstc. model. User;

Importcom. opensymphony. xwork2.ActionSupport;

 

Public classLoginAction extends ActionSupport {

Private boolean success;

Private String message;

Private User user;

Public String execute () throws Exception {

If (user. getUsername (). equals (admin) & user. getPassword (). equals (admin )){

This. success = true;

This. message = Your account is + user. getUsername () + password: + user. getPassword ();

} Else {

This. success = false;

This. message = sorry, unauthorized users cannot log on to this system !;

}

Return SUCCESS;

}

 

Public boolean isSuccess (){

Return success;

}

 

Public void setSuccess (boolean success ){

This. success = success;

}

 

Public String getMessage (){

Return message;

}

 

Public void setMessage (String message ){

This. message = message;

}

 

Public User getUser (){

Return user;

}

 

Public void setUser (User user ){

This. user = user;

}

}

In the Action class, you must specify a boolean variable success and an optional String variable msg. The former is used to report to ExtJS whether the entire processing result is successful, the latter is used to prompt users. After defining these two variables, you also need to generate the setter and getter methods. SUCCESS can be normally fed back to the entire Action class.

 

4. Configure struts. xml. Pay attention to extends = "json-default"

 

 

-// Apache Software Foundation // DTD StrutsConfiguration 2.0 // EN

Http://struts.apache.org/dtds/struts-2.0.dtd>

 

 

 

 

 

The type of result must be specified as json. Once a Result of the json type is specified for an Action, the Result does not need to be mapped to any view resource, because the JSON plug-in is responsible for serializing the information in the Action into a JSON string, return the string to the browser page that sends an asynchronous request.

 

Simply put, the JSON plug-in allows us to call an Action step in JavaScript on the client page, and the Action no longer needs to use view resources to display the status information in the Action, the JSON plug-in is responsible for returning the status information in the Action to the calling page. In this way, Ajax interaction can be completed.

 

There are also two points to note:

1: In the configuration of struts. i18n. encoding constant must be specified as UTF-8 encoding, this is because Ajax POST requests are encoded in the form of UTF-8;

2: When configuring a package, you must inherit the json-default instead of the original struts-default package. This is because only the json type Result is available under this package.

 

Once a logic is configured as the json type, this means that the logical view does not need to specify the physical view resource, because the json plug-in will serialize the Action and send it to the client.

 

5. Configure struts2 in the web. xml file

Xmlns = http://java.sun.com/xml/ns/javaee

Xmlns: xsi = http://www.w3.org/2001/XMLSchema-instance

Xsi: schemaLocation = http://java.sun.com/xml/ns/javaee

Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>

Struts2

Org. apache. struts2.dispatcher. ng. filter. StrutsPrepareAndExecuteFilter

 

 

Struts2

/*

 

Index. jsp

 

 

 

6.access is the front page, which includes the login.html (login interface), login. js (javascript code), and index. jsp (interface returned after successful login)

Login.html

 

 

 

 

 

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.