简体   繁体   English

Java Spring Framework,最佳实践,用于在JSP页面中填充面包屑的XML文件或Bean

[英]Java Spring Framework, What is best practice, XML File or Bean for Populating Bread Crumbs in JSP Page

I am new to Java and JSP. 我是Java和JSP的新手。 I have found information on this topic but some is rather dated so I was hoping to get a more current response. 我已经找到了有关该主题的信息,但是有些信息过时了,所以我希望能得到更多最新的答复。

My thought was to have a XML file set up like the following and based on the name attribute grab the info for that bread crumb. 我的想法是建立一个如下所示的XML文件,并基于name属性获取该面包屑的信息。

 <root>
      <view name="x">
           <crumb>
                <value1></value1>
                <value2></value2>
           </crumb>
           <crumb>
                 <value1></value1>
                 <value2></value2>
           </crumb>
     </view>
     <view name="y">
           <crumb>
                <value1></value1>
                <value2></value2>
           </crumb>
           <crumb>
                <value1></value1>
                <value2></value2>
           </crumb>
      </view>
 </root>

Is the Spring BeanFactory the best option or is there a better solution? Spring BeanFactory是最好的选择还是有更好的解决方案?

After trying various configurations for the bean.xml file I have come to this solution for my needs. 在尝试了bean.xml文件的各种配置之后,我根据自己的需要使用了该解决方案。 I'm sharing this resolution in hopes it may help someone with similar needs. 我分享此决议,希望它可以帮助有类似需求的人。 If anyone recognizes a cause for concern with server resources or pages being served slowly, and know of a better way, please share. 如果有人发现与服务器资源或页面缓慢提供有关的问题,并且知道更好的方法,请分享。

I configured the beans.xml like this: 我像这样配置beans.xml:

 <?xml version="1.0" encoding="UTF-8"?>

 <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
                       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <bean id="viewName" class="com.mycompany.beans.BreadCrumbs">
      <property name="crumbList">
          <list>
              <value>Home</value>
              <value>Admin</value>
          </list>
      </property>
  </bean>

  <bean id="Home" class="com.mycompany.beans.BreadCrumbs">
      <property name="prop1" value="value1"/>
      <property name="prop2" value="value2"/>
  </bean>   

  <bean id="Admin" class="com.mycompany.beans.BreadCrumbs">
      <property name="prop1" value="value1"/>
      <property name="prop2" value="value2"/>
  </bean>

The class method that parses the bean.xml is as follows. 解析bean.xml的类方法如下。

 public String crumb(String viewName){

    ApplicationContext appContext = new ClassPathXmlApplicationContext("breadCrumb.xml");
    BreadCrumbs crumbListObj = (BreadCrumbs) appContext.getBean(viewName);

    List y = crumbListObj.getCrumbList();
    String x="";

    for (int i=0; i < y.size(); i++){

      BreadCrumbs crumbpropsObj = (BreadCrumbs) appContext.getBean("" + y.get(i) + "");
      if (x.length() > 0){x += ",";}
      x = x + "," + y.get(i) + "," + crumbpropsObj.getVaule1() + "," + crumbpropsObj.getValue2(); 

    }

    return x;

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM