如何遍历/遍历嵌套的JAXB对象以在JSP页面中显示它?

时间:2013-07-24 06:13:27

标签: java jsp soap jaxb unmarshalling

我构建了肥皂请求并收到了回复。我使用JAXB上下文解组响应并获取包含响应的对象。

JAXBContext jc = JAXBContext.newInstance(ResponseMessage.class);
Unmarshaller um = jc.createUnmarshaller();
ResponseMessage output = (NumberResponseMessage)um.unmarshal(soapResponse.getSOAPBody().extractContentAsDocument());

现在我可以通过get ..()/ .. getValue()方法访问响应对象(输出)中的数据并将其打印在控制台中。但是当我尝试通过将响应对象添加到JSP页面的ModelAndView属性来发送响应对象时,会出现问题。

Model model.addAttribute("response", output);

我得到的错误是 - JSP不知道如何迭代给定的项目(因为我尝试循环)。

<c:forEach var ="Res" items="${output}">
<td>${Res.TransactionId}</td>
</c:forEach>

由于响应对象包含数百个数据,因此内部JAXB&lt;&gt;数组,它不可能将所有数据设置为用户bean然后访问。

有人可以建议我如何在JSP页面中迭代响应对象吗?

5 个答案:

答案 0 :(得分:3)

查看link中的代码示例。

它使用std::string::find_first_not_of,该示例实际显示了您想要做的事情! (这有多酷?)

根据@jogojapan

的建议,从链接中复制示例代码
// string::find_first_not_of
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>        // std::size_t

int main ()
{
  std::string str ("look for non-alphabetic characters...");

  std::size_t found = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz ");

  if (found!=std::string::npos)
  {
    std::cout << "The first non-alphabetic character is " << str[found];
    std::cout << " at position " << found << '\n';
  }

  return 0;
}

答案 1 :(得分:1)

你可以写一个正则表达式来查找一个字符是否特殊。这是一个例子:

std::regex rx("[\\[]+|[\"]+|..."); // ... is your special characters

以不同方式创建每个模式(大写,小写,特殊字符)并使用

std::regex_match如果模式匹配整个或部分字符串,则基本上返回true。

答案 2 :(得分:1)

一个简单的解决方案是创建一个string特殊字符。

//some pseudo code
string password = inputedPassword;
string specialChars = "@#$%&*";

for(int i = 0; i < password.length; i++)
  for(int j = 0; j < specialChars.length; i++)
      if(password[i] == specialChars[j])
          return true; //there is a special char

 return false; //no special chars

答案 3 :(得分:1)

你最好为所有公平的特殊角色维护一张桌子;那么你可以验证不是其他类型的字母。我想你是C ++的新人;因此,您需要一个例子。请按以下步骤操作:

bool IsSpecialCharacter(char c)
{
  static special_chars[] = "~`!@#$%^&*()_+-=\\\/?><,.";
  char *p = &special_chars[0];
  while(*p) { 
   if(*p == c) return true;
  };
  return false;
}

我虽然这是keelar的建议。

答案 4 :(得分:0)

按照你的逻辑,这应该适合你

    bool islower= false;
    bool isNumber= false;
    bool isUpper= false;
    bool isSpecialChar=false;


        for (int count = 0; count < length; count++)
        {
            if(!islower(password[count])){

                 islower=true;
                 break;
               }
        }

    if(islower){
          for(int count = 0; count < length; count++)
              {
                if( isdigit(password[count])){
                    isNumber=true;
                    break;
                  }
              }

    }

    if(isNumber){
        for (int count = 0; count < length; count++)
        {
            if(isupper(password[count])){
               isUpper=true;
               break;
              }
        }

    } 


if(isUpper){
  static special_chars[] = "~`!@#$%^&*()_+-=\\\/?><,.";
  char *p = &special_chars[0];
  for (int count = 0; count < length; count++){
      for(int j=0;j<24;j++){
          if(p[j]==password[count]{
          isSpecialChar=true;
          break;       
         }
      }

 }

}

    if(isNumber&&isUpper&&islower&&length>=8 && isSpecialChar){

        return true;
    }else{
       return false;
    }