Spring正确调用属性文件

发布于 2024-10-28 21:06:19 字数 826 浏览 1 评论 0原文

我有一个 spring mvc 应用程序,我正在使用扩展 AbstractPdfView 的类渲染一些 pdf。我有几个 pdf 文件,我认为创建一个辅助类来放置一些常用功能是有意义的。然后我决定将任何输出文本添加到 messages_en.properties 文件中。如何从我的助手类访问此文件?现在我正在手动创建我的助手类的实例。看起来像这样:

   public class PdfEarningsRecordView extends AbstractPdfView {

    @Override
    protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {

        HelperClass helper = new HelpderClass();
......

我尝试让 Helper 扩展 ApplicationContextAware 但总是返回 null。我还尝试了以下方法,得到了相同的结果:

@Autowire
private ApplicationContext context;
header = context.getMessage("myHeader", null, Locale.getDefault());

我觉得在手动创建 HelperClass 时我也没有正确使用 Spring。任何提示将不胜感激。

谢谢

I have a spring mvc application and I am rendering some pdfs using classes that extend AbstractPdfView. I have several pdfs and I thought it would make sense to create a helper class to put some common functionality. I then decided I wanted to add any output text to my messages_en.properties file. How do I access this file from my helper class? Right now I am creating an instance of my helper class manually. Looks like this:

   public class PdfEarningsRecordView extends AbstractPdfView {

    @Override
    protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {

        HelperClass helper = new HelpderClass();
......

I tried having the Helper extend ApplicationContextAware but always returned null. I also tried the following with the same result:

@Autowire
private ApplicationContext context;
header = context.getMessage("myHeader", null, Locale.getDefault());

I feel like I am not using Spring correctly when creating the HelperClass manually as well. Any tips would be appreciated.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白鸥掠海 2024-11-04 21:06:19

AbstractPdfViewApplicationObjectSupport 的子类,它有一个有用的 getMessageSourceAccessor() 方法,该方法返回一个 MessageSourceAccessor,是从框架获取消息的最简单方法。只需将其传递给您的帮助程序类:

public class PdfEarningsRecordView extends AbstractPdfView {

@Override
protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer, HttpServletRequest request, HttpServletResponse response) throws Exception {

    HelperClass helper = new HelperClass(getMessageSourceAccessor());

然后帮助程序就可以相应地使用它。

请注意,为了使其正常工作,必须正确初始化 PdfEarningsRecordView 对象。 Spring 通常会在启动期间通过调用其 ApplicationObjectSupport.setApplicationContext() 来为您完成此操作,但如果您自己实例化 PdfEarningsRecordView,则无论出于何种原因,您都必须自己调用该方法。

AbstractPdfView is a subclass of ApplicationObjectSupport, which has a useful getMessageSourceAccessor() method, which returns a MessageSourceAccessor, which is the easiest way to get messages from the framework. Just pass that to your helper class:

public class PdfEarningsRecordView extends AbstractPdfView {

@Override
protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer, HttpServletRequest request, HttpServletResponse response) throws Exception {

    HelperClass helper = new HelperClass(getMessageSourceAccessor());

The helper can then use that accordingly.

Note that in order for this to work, the PdfEarningsRecordView object must be properly initialized. Spring will generally do this for you, by calling its ApplicationObjectSupport.setApplicationContext() during startup, but if you instantiate a PdfEarningsRecordView yourself, for whatever reason, you'll have to call that method yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文