使用应用程序上下文感知将数据从 servlet 传递到 spring

发布于 2024-12-20 22:01:45 字数 2259 浏览 0 评论 0原文

我正在尝试在我的 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 技术交流群。

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

发布评论

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

评论(3

攒眉千度 2024-12-27 22:01:45

您确定不想使用 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());

故事与诗 2024-12-27 22:01:45

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.

卷耳 2024-12-27 22:01:45

在你的servlet中

@Autowired
private ApplicationContext ctx;

@Autowired
private MyDao myDao;

@Override
public void init() throws ServletException {
        WebApplicationContextUtils.getWebApplicationContext(super.getServletContext()).getAutowireCapableBeanFactory().autowireBean(this);
}

in your servlet

@Autowired
private ApplicationContext ctx;

@Autowired
private MyDao myDao;

@Override
public void init() throws ServletException {
        WebApplicationContextUtils.getWebApplicationContext(super.getServletContext()).getAutowireCapableBeanFactory().autowireBean(this);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文