【问题标题】:Integrate Spring Beans and JSP Tags集成 Spring Beans 和 JSP 标签
【发布时间】:2017-05-05 04:42:23
【问题描述】:

是否可以在 Spring 中以某种方式集成 Spring bean 和 JSP 标签。那就是使用 Spring bean 作为 JSP 标签?如果是这样,这是一个好/可怕的主意吗?所以这样的事情是可能的:

@Component
public class SomeBean extends SimpleTagSupport {

    @Autowired
    SomeBean someBean;

    @Override
    public void doTag() throws JspException, IOException {
        getJspContext().getOut().write(someBean.doSomething());
    }
}

我会在 tags.tld 中做什么来让它使用 Spring bean 而不是创建一个不会注入 someBean 的新实例?还是别的什么?

【问题讨论】:

    标签: java spring taglib


    【解决方案1】:

    InternalResourceViewResolver 允许您使用 exposition 将 bean 导出到 jsp 上下文中。例如,如果您想公开 bean 列表,您应该在 dispatcher-servlet.xml 中配置视图解析器:

       ...
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
           <property name="prefix" value="/WEB-INF/views/"/>
           <property name="suffix" value=".jsp"/>
           <property name="exposedContextBeanNames">
               <list>
                   <value>someBean</value>
               </list>
           </property>
       </bean>
       ...
    

    其中someBean 是您要导出到jsp 上下文的bean 的名称。你甚至可以暴露所有个spring beans:

       ...
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
           <property name="prefix" value="/WEB-INF/views/"/>
           <property name="suffix" value=".jsp"/>
           <property name="exposeContextBeansAsAttributes" value="true"/>
       </bean>
       ...
    

    这允许您通过 bean 的名称从 jsp 上下文访问 spring bean。

    假设您的标签的处理程序如下所示:

    package com.mytags;
    public class SomeTag extends SimpleTagSupport {
    
        private SomeBean bean;
    
        @Override
        public void doTag() throws JspException, IOException {
            getJspContext().getOut().write(bean.doSomething());
        }
    
        public SomeBean getBean(){...}
        public void setBean(SomeBean bean){...}
    }
    

    然后在 tld 中你的标签将被描述为下一个方式:

    ...
    <tag>
        <name>someTag</name>
        <tag-class>com.mytags.SomeTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>bean</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
            <type>com.mybeans.SomeBean</type>
        </attribute>
    </tag>
    ...
    

    注意,您必须将rtexprvalue 指定为true,因为您会将bean 作为EL 表达式传递到标签中。现在你可以在jsp中使用这个标签了:

    <%@ taglib prefix="mytags" uri="http://mytags.com" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
      <head>
        <title></title>
      </head>
      <body>
        <mytags:someTag bean="${someBean}"/>
      </body>
    </html>
    

    但实际上直接从标签的处理程序访问 spring bean 并不是一个好主意。标签必须知道如何显示数据,但应该从如何获取这些数据中抽象出来。更好地准备您希望在控制器中显示的所有数据并将其作为模型传递给 jsp。

    【讨论】:

    • 感谢您写出如此全面的答案,正是我想要的。
    猜你喜欢
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多