2 qq 23039605 qq_23039605 于 2016.09.21 09:14 提问

把 servlet 配置成 spring bean, 如何让 servlet 还可以正常访问呢 1C

当我把 servlet 配置成spring bean 对象,但是 访问改 servlet 却 报 404 错误,
我想知道还需要配置什么,可以正常访问改servlet ?

 <bean name="/login.do" class="com.test.LoginServlet" />

7个回答

dwangc
dwangc   2016.09.21 09:24

我是配置在web.xml里面的,可以正常访问,话说可以配置成bean吗

qq_23039605
qq_23039605 回复dwangc: spring可以管理servlet,用simpleservlethandleradapter
8 个月之前 回复
qq_23039605
qq_23039605 加了,但是 还是会报 404,
大约一年之前 回复
dwangc
dwangc 回复qq_23039605: <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml </param-value> </context-param>这个有没有加?
大约一年之前 回复
qq_23039605
qq_23039605 可以 配置成 bean 的,我在项目里看到了,而且可以正常访问,但我不知道怎么配置的
大约一年之前 回复
chuang102815
chuang102815   2016.09.21 09:29

web.xml里面加载spring文件了吗

qq_23039605
qq_23039605 加了,但是 还是会报 404,
大约一年之前 回复
qq592304796
qq592304796   2016.09.21 09:30

直接配置在spring的拦截器里面不就行了?何必这么麻烦

qq_23039605
qq_23039605   2016.09.21 09:36

加的,如下代码:


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        classpath:applicationcontext.xml
        classpath:applicationcontext-*.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
u013693144
u013693144   2016.09.21 09:46

确定是name不是id??

qq_23039605
qq_23039605 看一下,我贴的代码 如下:
大约一年之前 回复
u013693144
u013693144 web.xml配置DispatchServlet。。。。
大约一年之前 回复
u013693144
u013693144 web@requestmapping(“test”) 注解在你的类上和@requestmapping(“loginServlet”) 方法上,localhost:8080/test/loginServlet...
大约一年之前 回复
qq_23039605
qq_23039605 我就是想 知道,它是 怎么能够 访问 spring 配置bean 对象的 servlet?
大约一年之前 回复
u013693144
u013693144 回复qq_23039605: 这配置只是把servlet交给容器管理,后台根据name/id 取出该bean吧,没整合过servlet,我觉着要在web中配置,或者为什么不使用spring的web的一套东西
大约一年之前 回复
qq_23039605
qq_23039605 确定是 name? name 和 id 都可以作为 bean对象标识符
大约一年之前 回复
qq_23039605
qq_23039605   2016.09.21 11:33

web.xml 内容如下:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <servlet>
        <servlet-name>spring-web</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationcontext.xml</param-value> 
            </init-param> -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring-web</servlet-name>
        <url-pattern>*.jspx</url-pattern>
        <!-- <url-pattern>*.*</url-pattern> -->
    </servlet-mapping>


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        classpath:applicationcontext.xml
        classpath:applicationcontext-*.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classPath:log4j.properties</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener> -->
    <error-page>
        <error-code>404</error-code>
        <location>/ui/jsp/error.jsp</location>
    </error-page>
    <listener>
        <listener-class>com.tarena.excel.listener.ExampleListener</listener-class>
    </listener>

<!--    <filter>
        <filter-name>request filter</filter-name>
        <filter-class>com.tarena.servlet.RequestFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>request filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 -->    
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>
u013693144
u013693144   2016.09.21 11:39

我之前学习的时候配的一个 spring springmvc 项目,你看一下吧

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
  <display-name>CloudStorage</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml,classpath:spring-mvc.xml</param-value>
  </context-param>
    <filter>
    <description>字符集过滤器</description>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <description>字符集编码</description>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <listener>
    <description>spring监听器</description>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>
  <servlet>
    <description>spring mvc servlet</description>
    <servlet-name>springMvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <description>spring mvc 配置文件</description>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>/index</welcome-file>
  </welcome-file-list>
  <!--配置  org.springframework.web.filter.HiddenHttpMethodFilter 把post请求转为delete 或者put请求 -->
 <filter>
   <filter-name>HiddenHttpMethodFilter</filter-name>
   <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
 </filter>
  <filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!--配置ServletContextListener-->
  <listener>
    <listener-class>Listener.ContextListener</listener-class>
  </listener>
  <!--配置HttpSessionListener-->
  <listener>
    <listener-class>Listener.SessionListener</listener-class>
  </listener>
  <!--设置session过期时间,默认30分钟-->
  <session-config>
    <session-timeout>30</session-timeout>
  </session-config>
</web-app>
qq_23039605
qq_23039605 回复当你途径我的盛放: 谢谢,查到方式了,通过 委托过滤器代理,是可以实现的,实地测试过了,
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!