English 中文(简体)
向 Java翻译RoR观点
原标题:Translation of a RoR view to Java

理由 我正试图将以下的RoR观点守则转化为普惠制观点:

<代码>List<Object>bjectcontains the data I 我想在3栏中显示

<% 
modulo_objects = @objects.length % 3
base = @objects.length / 3
base = base.ceil
case modulo_objects
  when 0
 cols = [base, base, base]
  when 1
 cols = [base, base + 1, base]
  when 2
 cols = [base + 1, base, base + 1]
end

counter = 0
%>

<% 3.times do |i| %>
 <td width="220" align="center" style="padding-right: 15px;">
      <% cols[i].times do %>
  <h1><a href="/objects/show/<%= @objects[counter].urlname %>" ><%= @objects[counter].name %></a></h1>
  <% counter = counter + 1 %>
  <% end %>
 </td>
<% end %>

这是我迄今为止所看到的:

#{extends  main.html  /}
%{
modulo_objects = objects.size() % 3
base = objects.size() / 3
base = Math.ceil(base)
if(modulo_objects == 0)
    cols = [base, base, base]
else if(modulo_objects == 1)
    cols = [base, base + 1, base]
else if(modulo_objects == 2)
    cols = [base + 1, base, base + 1]
endif

counter = 0

}%

 #{list items:1..3, as: i }
     <td width="220" align="center" style="padding-right: 15px;">
         #{list items:cols[i]}
            <a href="@{Objects.show(objects.get(counter).name.replaceAll(" ", "-"))}" >${objects.get(counter).name}</a>
            %{ counter = counter + 1 }%
         #{/list}
     </td>
 #{/list}

The idea is to keep the items organised in 3 columns like 1|0|1 4|5|4 or 5|4|5 for example, I don t really understand if #{list items:cols[i]} will reproduce ruby s cols[i].times do.

迄今为止, Java的观点没有两个以上内容。

最佳回答
  public static List<Object>[] splitIn(List<Object> objects, int i) {

    int base_objects = objects.size() / i;
    int modulo_objects = objects.size() % i;
    int[] colSize = new int[i];

    switch (modulo_objects) {
        case 0:
            colSize[0] = base_objects;
            colSize[1] = base_objects;
            colSize[2] = base_objects;
            break;
        case 1:
            colSize[0] = base_objects;
            colSize[1] = base_objects + 1;
            colSize[2] = base_objects;
            break;
        case 2:
            colSize[0] = base_objects + 1;
            colSize[1] = base_objects;
            colSize[2] = base_objects + 1;
            break;
    }

    List<Object>[] columns = new List[i];

    int count = 0;
    for (int x = 0; x < i; x++) {
        List<Object> col_objects = new ArrayList();
        int colCount = 0;
        while (colCount < colSize[x]) {
            Object Object = (Object) objects.get(count);
            col_objects.add(Object);
            colCount++;
            count++;
        }
        columns[x] = col_objects;
    }
    return columns;
}






#{list cols, as: column }
  <td width="220" align="center" style="padding-right: 15px;">
      #{list column, as: object }
                <h1><a href="@{Objects.show(object.urlName())}" >${object.name}</a></h1>
      #{/list}
   </td>
   #{/list}
问题回答

只是评论(无特权):

i 确实认为,上述准则应当交给你的控制员,因为编码风格使你的MVC框架无效。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签