前端代码演示
<%--前端带上参数请求struts.xml配置文件中的hello--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>index</title>
<%@include file="./common.jsp"%>
</head>
<body>
<form class="layui-form" action="hello" method="post">
<div class="layui-form-item" style="margin: 10px">
<div class="layui-inline">
<label class="layui-form-label">用户名:</label>
<div class="layui-input-inline" style=" 200px;">
<input id="ename" type="text" name="user.userId" placeholder=" 请输入用户名" autocomplete="off" class="layui-input">
</div>
<label class="layui-form-label">编号:</label>
<div class="layui-input-inline" style=" 200px;">
<input id="empno" type="text" name="user.userName" placeholder=" 请输入编号" autocomplete="off" class="layui-input">
</div>
<div class="layui-input-inline" style=" 100px;">
<button type="submit" class="layui-btn layui-btn-normal"><i class="layui-icon layui-icon-search"></i> 刷新</button>
</div>
</div>
</div>
</form>
<a href="${path}/form.jsp">添加</a>
<table class="layui-table" lay-filter="empInfo">
<thead>
<tr>
<th><input type="checkbox"></th>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>省份</th>
<th>大区</th>
<th>小组信息</th>
<th>操作</th>
</tr>
<s:iterator value="#roleList">
<tr>
<!-- 值栈方式接收 -->
<td><input type="checkbox"></td>
<td><s:property value="empno"/></td>
<td><s:property value="ename"/></td>
<td><s:property value="age"/></td>
<td><s:property value="sex"/></td>
<td><s:property value="province"/></td>
<td><s:property value="deptld==1?'北京':'南京'"/></td>
<td><s:property value="groupu"/></td>
<td><a href="#">删除</a></td>
</tr>
</s:iterator>
</table>
</body>
</html>
配置代码演示
<!-- hello 获取到请求之后调用 com.struts.HelloAction类,带上参数,返回给前端 -->
<?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>
<!--设置请求的后缀-->
<!--<constant name="struts.action.extension" value="do,html"></constant>-->
<!--设置编码,解决中文乱码-->
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<!--namespace默认是根目录-->
<package name="default" extends="struts-default">
<!--友好页面跳转-->
<!--<default-action-ref name="default"></default-action-ref>-->
<!--name代表请求路径,class代表请求的类-->
<action name="hello" class="com.struts.HelloAction">
<!--相应结果和页面进行映射-->
<result name="success">/index.jsp</result>
<!--<result name="error">/error.jsp</result>-->
</action>
<action name="world" class="com.struts.WorldAction">
<result name="success">/form.jsp</result>
</action>
<!--<action name="default">-->
<!--<result>/error.jsp</result>-->
<!--</action>-->
</package>
</struts>
后端代码演示
/* Java代码业务逻辑操作*/
package com.struts;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.struts.outer.Result;
import com.struts.outer.Staff;
import com.struts.outer.User;
import com.struts.outer.Week;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @ClassName HelloAction
* @Description
* @Author CodeSheep
* @Date 2020/10/29 16:40
* @Version V1.0
* @Package com.example.demo.controller
*/
public class HelloAction implements Action {
private String name;
private String message;
private User user;
@Override
public String execute() throws Exception { //哎可死Q特
/*System.out.println(user.toString());
System.out.println("接收到的name是:"+name);
message = "123";
List<User> list = new ArrayList<>();
Collections.addAll(list,new User(1,"张三"),new User(2,"李四"));
ActionContext.getContext().put("roleList", list);*/
String json = "{"page": 1,"limit": 10}";
String body = HttpRequest.post(Week.URL + "/staff/list").timeout(2000)
.body(json)//JSONObject.toJSONString(requestDTO)
.execute()
.body();
JSONObject obj = (JSONObject)JSON.parse(body);
if(obj != null){
List<Staff> userList = JSON.parseArray(obj.getString("data"), Staff.class);
String resCode = obj.getString("count");
System.out.println(resCode);
List<Result> list = new ArrayList<>();
Result result = new Result(obj.getString("msg"), Integer.parseInt(obj.getString("code")), userList, Integer.parseInt(obj.getString("count")));
Collections.addAll(list,result);
ActionContext.getContext().put("roleList", list.get(0).getData());
}
return SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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;
}
}