Java --- Ajax application instance in the Struts2 framework, java --- ajaxstruts2
Ajax is a technology that updates some webpages without the need to reload the entire webpage.
I haven't touched ajax for a long time. I 'd like to take this opportunity to review it and write a simple example.
I. project structure:
Ii. Required jar packages
Iii. Specific Code:
1. web. xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts2</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
2. Action
UserLoginAction. java
Package Action; import com. opensymphony. xwork2.ActionSupport;
/**
* @ Author user: liuadmin
* @ Version date: December 8, 2017 6:54:00 AM
*
*/Public class UserLoginAction extends ActionSupport {private static final long serialVersionUID = 1L; 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;}/*** u Serlogin ** @ return SUCCESS */public String login () {System. out. println ("execute !!! "); If (" admin ". equals (this. username) & "123 ". equals (this. password) return "success"; else if (password = null & username = null) return "error"; else return "error ";}}
AjaxAction. java
Package Action; import java. util. hashMap; import java. util. map; import javax. servlet. http. httpServletRequest; import net. sf. json. JSONObject; import org. apache. struts2.interceptor. servletRequestAware; import com. opensymphony. xwork2.ActionSupport;
/**
* @ Author user: liuadmin
* @ Version date: December 8, 2017 6:56:00 AM
*
*/Public class AjaxAction extends ActionSupport implements ServletRequestAware {private static final long serialVersionUID = 1L; private HttpServletRequest request; private String result; public void setServletRequest (HttpServletRequest arg0) {this. request = arg0;} public String getResult () {return result;} public void setResult (String result) {this. result = result;}/*** process ajax requests ** @ return SUCCESS */public String executeAjax () {try {// obtain data String name = request. getParameter ("name"); int age = Integer. parseInt (request. getParameter ("age"); String position = request. getParameter ("position"); // store the data in the map and convert it to json type data. You can also manually construct a json type Map <String, object> map = new HashMap <String, Object> (); map. put ("name", name); map. put ("age", age); map. put ("position", position); JSONObject json = JSONObject. fromObject (map); // converts a map object to a json-type data result = json. toString (); // assign a value to the result and pass it to the page} catch (Exception e) {e. printStackTrace ();} return SUCCESS ;}}
3. struts. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <! -- Fix garbled characters --> <constant name = "struts. i18n. encoding "value =" UTF-8 "> </constant> <include file =" struts-default.xml "/> <package name =" struts "extends =" struts-default, json-default "> <action name =" login "class =" Action. userLoginAction "method =" login "> <result name =" success ">/success. jsp </result> <result name = "error">/error. jsp </result> </action> <action name = "executeAjax" method = "executeAjax" class = "Action. AjaxAction "> <result name =" fail "> </result> <! -- Return json data --> <result type = "json"> <param name = "root"> result <! -- Result is the variable name set in action and the data to be returned from the page, this variable must have the setter and getter methods --> </param> </result> </action> </package> </struts>
4. jsp
Index. jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Success. jsp
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
Error. jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Comments are made in important parts of the Code. comments can be made if you do not understand them.
Source code download: https://pan.baidu.com/s/1jIAarKM password: exgk