zoukankan      html  css  js  c++  java
  • struts2+ajax实现异步刷新

    login.jsp

     1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
     2 <%
     3     String path = request.getContextPath();
     4     String basePath = request.getScheme() + "://"
     5             + request.getServerName() + ":" + request.getServerPort()
     6             + path + "/";
     7 %>
     8 
     9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    10 <html>
    11     <head>
    12         <base href="<%=basePath%>">
    13 
    14         <title>My JSP 'login.jsp' starting page</title>
    15 
    16         <meta http-equiv="pragma" content="no-cache">
    17         <meta http-equiv="cache-control" content="no-cache">
    18         <meta http-equiv="expires" content="0">
    19         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    20         <meta http-equiv="description" content="This is my page">
    21         <!--
    22     <link rel="stylesheet" type="text/css" href="styles.css">
    23     -->
    24 
    25         <script type="text/javascript" src="js/jquery-1.8.3.js">
    26 </script>
    27     </head>
    28     <script type="text/javascript">
    29 function chkName() {
    30     var name = $("#username").val();       
    31     $.ajax( {
    32         url : "chkName.action",
    33         data : "name=" + name, //发送数据
    34         dataType : "text", //响应数据类型  
    35         type : "get", //请求发方式
    36         success : function(value) { //成功之后        
    37         $("#msg").html(value);
    38     }
    39     });
    40     }    
    41     </script>
    42     <body>
    43         <div id="show"></div>
    44         <form action="login.action" method="post">
    45             <div>
    46                 用户名:
    47                 <input name="username" type="text" value="" id="username"
    48                     onblur="chkName()" />
    49                 <span id="msg"></span>
    50             </div>
    51 
    52             <input type="submit" value="提交" />
    53         </form>
    54     </body>
    55 </html>

    struts.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4     "http://struts.apache.org/dtds/struts-2.3.dtd">
     5     
     6 <struts>
     7   <package name="default"  extends="struts-default" namespace="/">
     8     <action name="chkName" class="com.yh.myajax.LoginAction" method="chkName">
     9       <result type="stream">
    10         <param name="contentType">text/html;charset=utf-8</param>
    11         <param name="inputName">is</param>
    12       </result>
    13     </action>
    14   </package>
    15  </struts>

    LoginAction.java

     1 package com.yh.myajax;
     2 
     3 import java.io.ByteArrayInputStream;
     4 import java.io.InputStream;
     5 import java.io.UnsupportedEncodingException;
     6 
     7 import com.opensymphony.xwork2.ActionSupport;
     8 
     9 public class LoginAction extends ActionSupport {
    10     private String name;
    11     private InputStream is;   
    12     public String chkName(){
    13         String text;
    14         if("sa".equals(name)){
    15             text="用户已存在!";
    16         }else{
    17             text="此用户可用!";
    18         }
    19         try {
    20             this.is=new ByteArrayInputStream(text.getBytes("utf-8"));
    21         } catch (UnsupportedEncodingException e) {
    22             // TODO Auto-generated catch block
    23             e.printStackTrace();
    24         }
    25         return SUCCESS;       
    26     }   
    27     public String getName() {
    28         return name;
    29     }
    30     public void setName(String name) {
    31         this.name = name;
    32     }
    33     public InputStream getIs() {
    34         return is;
    35     }
    36     public void setIs(InputStream is) {
    37         this.is = is;
    38     }   
    39 }

    web.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
     3  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">
     4  <filter>
     5   <filter-name>struts2</filter-name>
     6   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
     7  </filter>
     8  <filter-mapping>
     9   <filter-name>struts2</filter-name>
    10   <url-pattern>/*</url-pattern>
    11  </filter-mapping>
    12  <welcome-file-list>
    13   <welcome-file>index.jsp</welcome-file>
    14  </welcome-file-list>
    15  <login-config>
    16   <auth-method>BASIC</auth-method>
    17  </login-config>
    18 </web-app>

    需要加载的包

    运行效果

  • 相关阅读:
    【ElasticSearch】异常错误
    【CentOS7】系统设置
    【Ubuntu 18.04.03_64】系统配置
    【MySql】语法学习
    【ElasticSearch】聚合使用学习
    【Spring Boot】Spring Security登陆异常出路
    【ElasticSearch】查询使用学习
    Spring boot X-Frame-Options 异常 a frame because it set 'X-Frame-Options' to 'deny'
    【Thymeleaf】使用学习
    【MySql】日期时间
  • 原文地址:https://www.cnblogs.com/jimorulang/p/5591063.html
Copyright © 2011-2022 走看看