如何将 spring 服务自动连接到 jsp?
How to autowired spring service into a jsp?
如标题所述,我需要在 page.jsp..
"我知道不建议这样做 "
<%@ 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 消息和 select 中的 123 选项..
感谢您的任何建议..
我想你可能不会在JSP中使用@Autowired注解。但是你已经使用了一些支持class。我可以帮助您解决这些问题:
解决方案一:
如果您正在使用 Spring MVC,
只需使用 ModelAndView将您的服务传递给 JSP。
示例:
假设您有 控制器:
@Controller
public void TestController{
@Autowired
private TestService tService;
@RequestMapping("someURL")
public ModelAndView displayPage{
//do some stuff
return new ModelAndView("myView").addObject("tService",tService);
}
}
JSP:
<html>
...
${tService.myMethodIWantToUse(..)}
...
</html>
方案二:
在您的 JSP 中使用 ApplicationContext 来访问这样的任何 bean
示例:
@Component("tsc")
public void TestServiceClass{
//Your code goes here
}
里面JSP:
ApplicationContext aC = RequestContextUtils.getWebApplicationContext(request);
TestServiceClass tsc1 = (TestServiceClass) aC.getBean("tsc");
//Code to access your service Classes methods.
希望这会有所帮助。
你甚至不应该尝试这样做......
JSP 被 servlet 容器翻译成 java 源代码并编译成 java 类,Java EE 规范没有说明它们的去向,所以你不能 spring 扫描它们,所以注释不是一个选项
更糟糕的是,JSP 不能是 Spring bean,因为它们是由 servlet 容器在应用程序上下文之外创建的,因此 XML 注入也无法工作。
甚至无法使用完整的 AspectJ,因为您再次无法控制 JSP 类 所在的位置,因此您甚至无法对它们使用加载时编织器。
问题不在于"that's not recommended to do it ",而是JSP是由servlet容器管理的特殊类。您可以在 scriplet 中使用 Java 代码,但您无法正常管理它们 Java 类.
顺便说一句,更一般地说,您不认为有充分的理由不推荐在 scriptlet 中使用太多 Java 代码吗?
您想从 JPS 页面调用 spring 服务方法吗?..
只需按照以下步骤操作,它对我有用...
假设您要访问 AnimalService
class.
第一步:
AbstractApplicationContext appContext = new AnnotationConfigApplicationContext(AnimalService.class);
如果您的 AnimalService 依赖于另一个 class 比方说 FoodService
..那么第 1 步应该如下所示..
AbstractApplicationContext appContext = new AnnotationConfigApplicationContext(AnimalService.class,FoodService.class);
简而言之,您需要在此处添加所有依赖 class。
第 2 步:
AnimalService animalservice = appContext.getBean(AnimalService.class);
animalservice.yourMethod();
里面 jsp:
TestService testService = ApplicationContextProvider.getApplicationContext().getBean(TestService.class);
testService.testMethod();
您还可以为 getBean()
方法使用 String
参数(服务名称)
Spring 可以(现在)通过将您的 bean 名称公开到请求上下文中来为您完成此操作,您只需要在 viewResolver 中配置它。
发件人:https://raibledesigns.com/rd/entry/spring_mvc_jstlview_and_exposecontextbeansasattributes
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="exposeContextBeansAsAttributes" value="true"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
如标题所述,我需要在 page.jsp.. "我知道不建议这样做 "
<%@ 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 消息和 select 中的 123 选项..
感谢您的任何建议..
我想你可能不会在JSP中使用@Autowired注解。但是你已经使用了一些支持class。我可以帮助您解决这些问题:
解决方案一:
如果您正在使用 Spring MVC,
只需使用 ModelAndView将您的服务传递给 JSP。
示例:
假设您有 控制器:
@Controller
public void TestController{
@Autowired
private TestService tService;
@RequestMapping("someURL")
public ModelAndView displayPage{
//do some stuff
return new ModelAndView("myView").addObject("tService",tService);
}
}
JSP:
<html>
...
${tService.myMethodIWantToUse(..)}
...
</html>
方案二:
在您的 JSP 中使用 ApplicationContext 来访问这样的任何 bean
示例:
@Component("tsc")
public void TestServiceClass{
//Your code goes here
}
里面JSP:
ApplicationContext aC = RequestContextUtils.getWebApplicationContext(request);
TestServiceClass tsc1 = (TestServiceClass) aC.getBean("tsc");
//Code to access your service Classes methods.
希望这会有所帮助。
你甚至不应该尝试这样做...... JSP 被 servlet 容器翻译成 java 源代码并编译成 java 类,Java EE 规范没有说明它们的去向,所以你不能 spring 扫描它们,所以注释不是一个选项
更糟糕的是,JSP 不能是 Spring bean,因为它们是由 servlet 容器在应用程序上下文之外创建的,因此 XML 注入也无法工作。
甚至无法使用完整的 AspectJ,因为您再次无法控制 JSP 类 所在的位置,因此您甚至无法对它们使用加载时编织器。
问题不在于"that's not recommended to do it ",而是JSP是由servlet容器管理的特殊类。您可以在 scriplet 中使用 Java 代码,但您无法正常管理它们 Java 类.
顺便说一句,更一般地说,您不认为有充分的理由不推荐在 scriptlet 中使用太多 Java 代码吗?
您想从 JPS 页面调用 spring 服务方法吗?.. 只需按照以下步骤操作,它对我有用...
假设您要访问 AnimalService
class.
第一步:
AbstractApplicationContext appContext = new AnnotationConfigApplicationContext(AnimalService.class);
如果您的 AnimalService 依赖于另一个 class 比方说 FoodService
..那么第 1 步应该如下所示..
AbstractApplicationContext appContext = new AnnotationConfigApplicationContext(AnimalService.class,FoodService.class);
简而言之,您需要在此处添加所有依赖 class。
第 2 步:
AnimalService animalservice = appContext.getBean(AnimalService.class);
animalservice.yourMethod();
里面 jsp:
TestService testService = ApplicationContextProvider.getApplicationContext().getBean(TestService.class);
testService.testMethod();
您还可以为 getBean()
方法使用 String
参数(服务名称)
Spring 可以(现在)通过将您的 bean 名称公开到请求上下文中来为您完成此操作,您只需要在 viewResolver 中配置它。
发件人:https://raibledesigns.com/rd/entry/spring_mvc_jstlview_and_exposecontextbeansasattributes
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="exposeContextBeansAsAttributes" value="true"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>