strust2配置action的完整过程

后台action类 RoleManagerAction.java

package com.ybcz.sys.sysaction;

import com.ybcz.common.util.BaseAction;
import com.ybcz.common.util.UserInfo;
import com.ybcz.sys.role.service.SysRoleService;
import org.apache.struts2.interceptor.RequestAware;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: what
 * @Title: RoleManagerAction
 * @Package: com.ybcz.sys.sysaction
 * @Copyright: 
 * @Company: 
 * @author: NEIL
 * @date: 2015/9/6
 * @version: V1.0
 */
public class RoleManagerAction extends BaseAction{

    private static final long serialVersionUID = 1227393768429781621L;

    private SysRoleService sysRoleService;
    private UserInfo userInfo;              //使用json返回对象
    public void setSysRoleService(SysRoleService sysRoleService) {
        this.sysRoleService = sysRoleService;
    }

    public UserInfo getUserInfo() { return userInfo;}

    public void setUserInfo(UserInfo userInfo) {this.userInfo = userInfo; }

    public String roleMain() throws Exception {
        return SUCCESS;
    }

    public String returnTestData(){
        userInfo = new UserInfo();
        userInfo.setLoginid("admin");
        userInfo.setUsername("刘aa");
        return SUCCESS;
    }

}

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>
    
    <package name="roleActionStruts" namespace="/com/ybcz/sys/role" extends="json-default">
        <action name="roleMain" class="com.ybcz.sys.sysaction.RoleManagerAction" method="roleMain">
            <result  name="success">roleMain.jsp</result>
        </action>
        <action name="returnTestData" class="com.ybcz.sys.sysaction.RoleManagerAction" method="returnTestData">
            <result  name="success" type="json"></result>
        </action>
    </package>

</struts>

index.html


<%--  Created by IntelliJ IDEA.  User: neil  Date: 2015/6/13  Time: 17:17  To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
  <head>
    <title></title>
    <script src="assets/js/jquery-2.1.4.min.js"></script>
  </head>
  <body  class="page-body login-page">
  <div class="container">
    <h3><a href="com/ybcz/sys/role/roleMain">角色管理</a></h3>
  </div>
  </body>
</html>

roleMain.jsp

<%--  Created by IntelliJ IDEA.  User: zwf  Date: 2015/9/13  Time: 14:46  To change this template use File | Settings | File Templates. --%> <%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>role角色列表-树形目录实例</title>
    <link href="../../../../assets/css/fancytree/win8-n/ui.fancytree.css" rel="stylesheet" type="text/css">
    <link href="../../../../assets/css/bootstrap.css" rel="stylesheet">
</head>
<body>

<div class="container">
    <h1>布局模板</h1>
    <button type="button" class="btn btn-success check-node" id="btn-check-node">显示Json测试数据
    <div id="test"/>
</div></body><script src="../../../../assets/js/jquery-2.1.4.min.js"></script><script type="text/javascript"> /*这里获取json数据*/ $(document).ready(function () { //通过选择器获得它的单机事件 $("#btn-check-node").click(function () { var str = ""; $.ajax({ url: 'returnTestData.action', type: 'post', data: "{}", dataType: 'json', success: function (data) { $(data.userInfo).each(function (i, value) { str = str + value.username + ","; }); str = str.substring(0, str.length - 1); $("#test").text(str); } }); }); });</script></html>


你可能感兴趣的:(strust2配置action的完整过程)