我正在使用Struts2开发一个Web应用程序。我从数据库中提取记录并以.json
格式显示。但我想以表格格式在JSP页面中显示详细信息。每当我在服务器上单独运行页面时,我都会得到结果。但是在struts.xml
中配置后,我没有以表格格式获取它。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="helloworld" extends="json-default">
<action name="jsondbsession" class="com.cisco.dbc.DbSessionService"
method="getDbSessions">
<result name="success">demo.jsp</result>
<result name="error">sessionError.jsp</result>
</action>
</package>
</struts>
正如您在struts.xml
文件中看到的,我已在动作标记中删除了 type="json"
。然后输出是一个没有记录的表。
但如果我写 type="json"
,那么我会以json格式获取数据,而不是以表格格式。
"sessionDetailsBeansList": [
{
"EVENT": "pmon timer",
"IDLE_MINS": 281,
"INST_ID": 1,
"LOGON": "17-APR-2015 11:18:21",
"MACHINE": "TKANTAPR-WS",
"MODULE": null,
"OSUSER": "SYSTEM",
"SERIAL": 1,
"SID": 87,
"SPID": "3296",
"SQL_HASH_VALUE": 0,
"STATUS": "ACTIVE",
"USERNAME": null
}"
这是我的angularjs代码。
<!DOCTYPE html>
<html>
<head>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<style>
tr {
text-align: center;
}
</style>
</head>
<body>
<div align="center" data-ng-app="myApp" data-ng-controller="DbSessionServiceController">
<table border="1">
<tr class="thead" style="font-size: large;text-align: center;">
<td>INTANCE ID</td>
<td>SID</td>
<td>SERIAL</td>
<td>USERNAME</td>
<td>SPID</td>
<td>OS USER</td>
<td>STATUS</td>
<td>MODULE</td>
<td>MACHINE</td>
<td>SQL HASH VALUE</td>
<td>LOGON</td>
<td>EVENT</td>
<td>IDLE MINS</td>
</tr>
<tr data-ng-repeat="x in names">
<td>{{ x.INST_ID }}</td>
<td>{{ x.SID }}</td>
<td>{{ x.SERIAL }}</td>
<td>{{ x.USERNAME }}</td>
<td>{{ x.SPID }}</td>
<td>{{ x.OSUSER }}</td>
<td>{{ x.STATUS }}</td>
<td>{{ x.MODULE }}</td>
<td>{{ x.MACHINE }}</td>
<td>{{ x.SQL_HASH_VALUE }}</td>
<td>{{ x.LOGON }}</td>
<td>{{ x.EVENT }}</td>
<td>{{ x.IDLE_MINS }}</td>
</tr>
</table>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('DbSessionServiceController', function($scope, $http) {
$http.get("http://localhost:8088/dbcui/jsondbsession.action?InstanceName=LOCAL").
success(function (response) {$scope.names = response.sessionDetailsBeansList;});
});
</script>
</body>
</html>
输出应采用表格格式。我在这里缺少什么?
答案 0 :(得分:1)
使用AngularJS时不需要特殊的Struts2配置。
为了开始使用Struts2和AngularJS,我建议您试用Struts2 AngularJS Maven Archetype来介绍这个主题以及如何配置它。
mvn archetype:generate -B -DgroupId=com.mycompany.mysystem \
-DartifactId=myWebApp \
-DarchetypeGroupId=org.apache.struts \
-DarchetypeArtifactId=struts2-archetype-angularjs \
-DarchetypeVersion=<CURRENT_STRUTS_VERSION> \
-DremoteRepositories=http://struts.apache.org
此原型为您生成一个带有示例AngularJS配置的初始项目堆栈。
答案 1 :(得分:0)
AngularJs 所需的响应是json类型,所以我们需要将对象转换为json字符串,为此你需要在 struts.xml 中配置Action类它返回一个json对象。
此配置如下:
<struts>
<package name="default" extends="json-default">
<action name="angularAction" class="com.action.AngularAction">
<result type="json">
<param name="root">personData</param>
<param name="excludeNullProperties">true</param>
<param name="noCache">true</param>
</result>
</action>
</package>
</struts>
在struts.xml文件中创建一个包,该文件扩展 json-default ,并将此包内的操作类的结果类型指定为json。 json-default包中包含拦截器以及JSON请求和响应的结果类型配置。