• Struts2+Ajax实现数据交互


    1.导入jar包

    struts核心包:

    json需要的包:

    2.配置web.xml

    <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>
    

    3.编写页面

    <%@ 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">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
    <script>
    	$(function(){
    		
    		$("#button").click(function(){
    						
    			var data = {
    					username: $("#username").val(),
    					
    					userpwd: $("#userpwd").val()
    										
    			}
    			
    					
    			$.post("login.action",data,function(msg){
    			
    				alert(msg);
    				
    			},'json');
    			
    					
    		})
    			
    	})
    
    </script>
    
    </head>
    <body>
    用户姓名:<input type="text" id ="username"><br><br>
    用户密码:<input type="text" id= "userpwd"><br><br>
    <input type="button" value="提交" id="button">
    </body>
    </html>
    
    

    4.配置struts.xml和Action类

    原生实现方法

        public class UserAction extends ActionSupport implements ServletRequestAware {
    
    	private String username;
    
    	private String userpwd;
    
    	private HttpServletRequest request;
    
    
    	public String getUsername() {
    		return username;
    	}
    
    	public void setUsername(String username) {
    		this.username = username;
    	}
    
    	public String getUserpwd() {
    		return userpwd;
    	}
    
    	public void setUserpwd(String userpwd) {
    		this.userpwd = userpwd;
    	}
    
    	@Override
    	public void setServletRequest(HttpServletRequest arg0) {
    
    		this.request = arg0;
    	}
    
    	public void executeAjax() throws IOException {
    
    		// 处理Ajax请求
    
    		String name = request.getParameter("username");
    		
    		String pwd = request.getParameter("userpwd");
    
    		HttpServletResponse response = ServletActionContext.getResponse();
    		
    		response.getWriter().print("hello"+username+"hello"+userpwd);
    		
    	}
    
        }
    
    

    使用插件

    <struts>
        <package name="hello" namespace="/" extends="struts-default,json-default">
            <action name="login" class="cn.jiangwenwen.action.UserAction" method="executeAjax">
                <result type="json">
                	 <param name="root">result</param>
                </result>
            </action>
        </package>
    </struts>
    
    
    public class UserAction extends ActionSupport{
    	
    	
    	private String username;
    	
    	private String userpwd;
    	
    	private String result;
    	
    	
    	public String executeAjax() {
    		
    		if("admin".equals(username)) {
    			result = "主人,欢迎回家!";
    		}else {
    			result = "警告,非法入侵!";
    		}
    		
    		return SUCCESS;
    	}
    	
    	
    	
    
    	public String getUsername() {
    		return username;
    	}
    
    	public void setUsername(String username) {
    		this.username = username;
    	}
    
    	public String getUserpwd() {
    		return userpwd;
    	}
    
    	public void setUserpwd(String userpwd) {
    		this.userpwd = userpwd;
    	}
    
    	public String getResult() {
    		return result;
    	}
    
    	public void setResult(String result) {
    		this.result = result;
    	}
    	
    	
    
    }
    
    
  • 相关阅读:
    程序向informix数据库插入text类型的中文乱码问题
    深入理解Java:注解(Annotation)基本概念
    ssm架构的理解
    队列的java实现
    栈的java实现
    LinkedList的实现原理
    ArrayList的实现
    快速排序
    数据结构之桶排序
    leetcode
  • 原文地址:https://www.cnblogs.com/jiangwenwen1/p/9458800.html
Copyright © 2020-2023  润新知