当我把 servlet 配置成spring bean 对象,但是 访问改 servlet 却 报 404 错误,
我想知道还需要配置什么,可以正常访问改servlet ?
<bean name="/login.do" class="com.test.LoginServlet" />
我是配置在web.xml里面的,可以正常访问,话说可以配置成bean吗
web.xml里面加载spring文件了吗
直接配置在spring的拦截器里面不就行了?何必这么麻烦
加的,如下代码:
<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>
确定是name不是id??
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>
我之前学习的时候配的一个 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>