【问题标题】:How to configure AngularJS file in Struts.xml?如何在 Struts.xml 中配置 AngularJS 文件?
【发布时间】:2015-04-17 10:42:24
【问题描述】:

我正在使用 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" in action 标签。那么输出就是一个没有记录的表。

但是如果我写 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>

输出应采用表格格式。我在这里错过了什么?

【问题讨论】:

  • 当然需要使用json结果类型。你是什​​么意思 - 然后我得到 json 格式的数据
  • 如果你执行 console.log($scope.names); 会发生什么成功后?
  • @AleksandrM 我已经写了下面的输出。它应该采用表格格式。
  • 在典型的 struts2-angularjs 应用程序中,使用约定或休息插件来调用类似 grails 的操作。
  • 你在哪里看到这个输出?所以你认为你的ng-repeat 不起作用?

标签: java json angularjs struts2


【解决方案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 配置的初始项目堆栈。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 2013-07-15
    相关资源
    最近更新 更多