使用应用程序上下文感知将数据从 servlet 传递到 spring
我正在尝试在我的 servlet 中实现 applicationContextAware。我有来自客户端的数据传入我的 servlet。从我的 servlet,我需要将其传递给具有 getter 和 setter 的 bean。我有我的 DAO,其中有 MYSQL 操作。
我的 applicationContext.xml 包含
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/bazaar_admin_portal" />
<property name="username" value="usrnm" />
<property name="password" value="pwd" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg index="0">
<ref bean="dataSource" />
</constructor-arg>
</bean>
<bean
class="org.dao.impl.TestDAOimpl"
id="TestDAO">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
我的 web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<description></description>
<display-name>TestServlet</display-name>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.controllers.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/Test</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
包含 在 doPost 方法下的 TestServlet 中,
private static ApplicationContext applicationContext = null;
public void setApplicationContext(ApplicationContext ctx)
throws BeansException {
applicationContext = ctx;
我有 getters 和 setters 类 Test.Also 接口 TestDAO 和实现该接口的 TestDAOimpl 类。
我想知道如何将数据从 servlet 传递到 spring 端...即设置数据,使 TestDAOimpl 能够插入到我的数据库中。
谢谢
I am trying to implement applicationContextAware in my servlet.I have the data from client side coming to my servlet.From my servlet I need to pass it to the beans which has getters and setters.I have my DAO s where I have MYSQL operations.
My applicationContext.xml has
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/bazaar_admin_portal" />
<property name="username" value="usrnm" />
<property name="password" value="pwd" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg index="0">
<ref bean="dataSource" />
</constructor-arg>
</bean>
<bean
class="org.dao.impl.TestDAOimpl"
id="TestDAO">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
My web.xml contains
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<description></description>
<display-name>TestServlet</display-name>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.controllers.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/Test</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
And in my TestServlet under doPost method
private static ApplicationContext applicationContext = null;
public void setApplicationContext(ApplicationContext ctx)
throws BeansException {
applicationContext = ctx;
I have getters and setters class Test.Also interface TestDAO and TestDAOimpl class which implements the interface.
I want to know how do I pass the data from my servlet to the spring side...i.e set the data which will enable the TestDAOimpl to insert into my DB.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您确定不想使用 Spring WebMVC 吗?它会自动处理你的问题。
然后在 POST 方法中尝试这个(它很慢,延迟初始化):
applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
Are you sure you don't want to use Spring WebMVC? It will handle your problem automatically.
Then try this in you POST Method (It's quite slow, init it lazily):
applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
ApplicationContextAware 使 bean 能够了解其应用程序上下文。阅读此处< /a> 了解更多信息。你可以使用 WebApplicationContextUtils WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc) ,获取应用程序上下文,使用 getBean 方法并调用 Dao。
ApplicationContextAware is for beans to be aware of their application context . Read here for more info . What you could so is use WebApplicationContextUtils WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc) , get the application context , use getBean method and invoke the Dao.
在你的servlet中
in your servlet