如何将Spring Service自动接线到JSP中? [英] How to autowired spring service into a jsp?

查看:66
本文介绍了如何将Spring Service自动接线到JSP中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题中所述,我需要在page.jsp中自动装配服务. "我知道不建议这样做"

As mentioned in the title, i need to autowire a service in my page.jsp.. "i know that's not recommended to do it "

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


<%@ page import="org.springframework.context.ApplicationContext"%>
<%@ page
    import="org.springframework.web.servlet.support.RequestContextUtils"%>

<%@ page import="com.fussa.fyby.service.Test"%>
<%@ page import="com.fussa.fyby.model.PIL_P_APPLCTN"%>


<%
    ApplicationContext ac = RequestContextUtils.getWebApplicationContext(request);
    Test s = (Test) ac.getBean("fussafyby");
    PIL_P_APPLCTN app = s.getByKey(13);
%>


<c:out value="azeerty"></c:out>

<c:out value="${ app.APPLCTN_CD }"></c:out>

<select name="listeGroupes" id="listeGroupes">

    <option value="123">123</option>

    <option value="${ app.APPLCTN_CD }">${ app.APPLCTN_CD }</option>
    <option value="123">${ s.afficher() }</option>

</select>

我的服务:

@Component("fussafyby")
@Transactional
public class Test {

    @Autowired
    private SessionFactory sessionFactory;

    protected Session getSession() {
        return sessionFactory.getCurrentSession();
    }

    public PIL_P_APPLCTN getByKey(int key) {
        return (PIL_P_APPLCTN) getSession().get(PIL_P_APPLCTN.class, key);
    }



    public String affiche() {
        return "FuSsA";
    }
}

仅显示 azeerty 消息和选择中的 123 选项.

Only azeerty message and 123 option in the select are shown..

谢谢您的建议.

推荐答案

您甚至不应该尝试这样做... 通过Servlet容器将JSP转换为Java源代码并编译为Java类,并且Java EE规范没有说明它们的去向,因此您无法对它们进行弹簧扫描,因此不能选择注释

You should not even try to do that... JSP are translated to java source and compiled to java classes by the servlet container, and the Java EE specification does not say where they go, so you cannot have spring scan them, so annotation is not an option

更糟糕的是,JSP不能是Spring Bean,因为它们是由应用程序上下文外部的servlet容器创建的,因此XML注入也不起作用.

Worse, the JSP cannot be Spring beans because they are created by the servlet container outside of the application context, so XML injection cannot work either.

甚至无法使用完整的AspectJ,因为您再一次无法控制JSP类的位置,因此甚至无法在它们上使用加载时织布器.

And even full AspectJ cannot be used because once again you have no control on where JSP classes lie so you cannot even use a load-time weaver on them.

问题不在于不建议这样做" ,而是JSP是由servlet容器管理的特殊类.您可以在scriplet中使用Java代码,但不能将它们作为普通Java类进行管理.

The problem is not that "that's not recommended to do it ", it is that JSP are special classes managed by the servlet container. You can use Java code in scriplets, but you cannot manage them as normal Java classes.

顺便说一句,更笼统地说,您不认为没有充分的理由不建议在scriptlet中推荐太多Java代码吗?

BTW, and more generaly, don't you think that there can be good reasons for not recommending too much Java code in scriptlets?

这篇关于如何将Spring Service自动接线到JSP中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆