浏览 2217 次
锁定老帖子 主题:spring集成servlet的疑惑
精华帖 (0) :: 良好帖 (0) :: 新手帖 (4) :: 隐藏帖 (1)
|
|
---|---|
作者 | 正文 |
发表时间:2011-09-01
<servlet> <description></description> <display-name>UserActionImpl2</display-name> <servlet-name>UserActionImpl2</servlet-name> <servlet-class>com.action.MyAction</servlet-class> </servlet> <servlet-mapping> <servlet-name>UserActionImpl2</servlet-name> <url-pattern>/user</url-pattern> </servlet-mapping>
package com.action; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; /** * Servlet implementation class UserAction */ public class MyAction extends HttpServlet { private static final long serialVersionUID = 1L; protected Servlet servlet; /** * @see HttpServlet#HttpServlet() */ public MyAction() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { servlet.service(request, response); } @Override public void init() throws ServletException { System.out.println("init"); String servletName = getServletConfig().getServletName(); WebApplicationContext webapplicationcontext = WebApplicationContextUtils .getRequiredWebApplicationContext(getServletContext()); this.servlet = (Servlet) webapplicationcontext.getBean(servletName); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { servlet.service(request, response); } } package com.action.impl; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.action.MyAction; import com.pojo.User; import com.services.UserServices; public class UserActionImpl2 extends MyAction { private static final long serialVersionUID = 1L; private static final int size = 5; private UserServices userServices; public void setUserServices(UserServices userServices) { this.userServices = userServices; } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } protected Servlet servlet; @Override public void init() throws ServletException { System.out.println("init children"); String servletName = getServletConfig().getServletName(); WebApplicationContext webapplicationcontext = WebApplicationContextUtils .getRequiredWebApplicationContext(getServletContext()); this.servlet = (Servlet) webapplicationcontext.getBean(servletName); } } 为什么web.xml里面一定得配置com.action.Myaction才能获得spring提供的servlet对象,配置子类UserActonImpl2,也是通过init从spring获取servlet对象,为什么userServices为NULL? 我对配置子类UserActonImpl2的理解:因为UserActonImpl2已经从web服务器获取到,然后通过init方法来对servlet注入对象!这样就不会为userServices注入对象了! 对配置父类com.action.Myaction不能很理解,求javaeye的哥哥姐姐们指点下,解释下这2者是怎么获取servlet对象的,还有就是怎么样可以在web.xml里面直接配置子类UserActonImpl2能够获取userServices? 语言表达能力有限,描述了N久,才这样,我是hold不住了,哥哥姐姐们一定的hold住 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2011-09-01
嗨,抽了跟烟,想了想,终于把这2天的疑惑解决了,
this.setUserServices((UserServices)webapplicationcontext.getBean("userServices")); 在子类的init方法里面加上上述代码就可以了,嗨,基础太差,伤不起啊! 对servlet的理解有限! |
|
返回顶楼 | |