首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

spring集成servlet的不解

2012-10-06 
spring集成servlet的疑惑servletdescription/descriptiondisplay-nameUserActionImpl2/display-n

spring集成servlet的疑惑

<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);}@Overridepublic 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;}@Overrideprotected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {}@Overrideprotected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}protected Servlet servlet;@Overridepublic 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住

热点排行