您的位置:首页技术文章
文章详情页

java - SSM框架,控制台没报错,在页面测试的时候报404

【字号: 日期:2023-11-20 10:52:42浏览:33作者:猪猪

问题描述

java - SSM框架,控制台没报错,在页面测试的时候报404

web.xml

<?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_3_0.xsd' version='3.0'>

<display-name>Archetype Created Web Application</display-name> <!-- Spring和mybatis的配置文件 --> <context-param>

<param-name>contextConfigLocation</param-name><param-value>classpath:spring-mybatis.xml</param-value>

</context-param> <!-- 编码过滤器 --> <filter>

<filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><async-supported>true</async-supported><init-param> <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> <!-- Spring监听器 --> <listener>

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

</listener> <!-- 防止Spring内存溢出监听器 --> <listener>

<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>

</listener>

<!-- Spring MVC servlet --> <servlet>

<servlet-name>SpringMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup><async-supported>true</async-supported>

</servlet> <servlet-mapping>

<servlet-name>SpringMVC</servlet-name><url-pattern>/</url-pattern>

</servlet-mapping>

<filter>

<filter-name>RespFilter</filter-name><filter-class>me.lihs.java.utils.ResponseFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>RespFilter</filter-name><url-pattern>/*</url-pattern>

</filter-mapping>

<welcome-file-list>

<welcome-file>/index.jsp</welcome-file><welcome-file>/index.html</welcome-file>

</welcome-file-list></web-app>

spring-mybatis.xml

<?xml version='1.0' encoding='UTF-8'?><beans xmlns='http://www.springframework.org/schema/beans'

xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:p='http://www.springframework.org/schema/p' xmlns:context='http://www.springframework.org/schema/context' xmlns:mvc='http://www.springframework.org/schema/mvc' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd'>

<!-- 自动扫描 --> <context:component-scan base-package='me.lihs.java'/> <!-- 引入配置文件 --> <bean class='org.springframework.beans.factory.config.PropertyPlaceholderConfigurer'>

<property name='location' value='classpath:jdbc.properties'/>

</bean>

<bean destroy-method='close'>

<property name='driverClassName' value='${driver}'/><property name='url' value='${dbUrl}'/><property name='username' value='${username}'/><property name='password' value='${password}'/><!-- 初始化连接大小 --><property name='initialSize' value='${initialSize}'/><!-- 连接池最大数量 --><property name='maxActive' value='${maxActive}'/><!-- 连接池最大空闲 --><property name='maxIdle' value='${maxIdle}'/><!-- 连接池最小空闲 --><property name='minIdle' value='${minIdle}'/><!-- 获取连接最大等待时间 --><property name='maxWait' value='${maxWait}'/>

</bean>

<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean class='org.mybatis.spring.SqlSessionFactoryBean'>

<property name='dataSource' ref='dataSource'/><!-- 自动扫描mapping.xml文件 --><property name='mapperLocations' value='classpath:me/lihs/java/mapping/*.xml'/>

</bean>

<!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean class='org.mybatis.spring.mapper.MapperScannerConfigurer'>

<property name='basePackage' value='me.lihs.java.dao'/><property name='sqlSessionFactoryBeanName' value='sqlSessionFactory'/>

</bean>

</beans>

spring-mvc.xml

<?xml version='1.0' encoding='UTF-8'?><beans xmlns='http://www.springframework.org/schema/beans'

xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:context='http://www.springframework.org/schema/context' xmlns:mvc='http://www.springframework.org/schema/mvc' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd'>

<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 --> <context:component-scan base-package='me.lihs.java.controller'/>

<!--避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id='mappingJacksonHttpMessageConverter'

class='org.springframework.http.converter.json.MappingJacksonHttpMessageConverter'><property name='supportedMediaTypes'> <list> <value>text/html;charset=UTF-8</value> </list></property>

</bean>

<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 --> <bean

class='org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter'><property name='messageConverters'> <list> <ref bean='mappingJacksonHttpMessageConverter'/> <!-- JSON转换器 --> </list></property>

</bean> <!-- 定义跳转的文件的前后缀 ,视图模式配置--> <bean class='org.springframework.web.servlet.view.InternalResourceViewResolver'>

<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 --><property name='prefix' value='/WEB-INF/jsp/'/><property name='suffix' value='.jsp'/>

</bean>

<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 --> <!--<bean id='multipartResolver'-->

<!--class='org.springframework.web.multipart.commons.CommonsMultipartResolver'>--><!--&lt;!&ndash; 默认编码 &ndash;&gt;--><!--<property name='defaultEncoding' value='utf-8'/>--><!--&lt;!&ndash; 文件大小最大值 &ndash;&gt;--><!--<property name='maxUploadSize' value='500000000'/>--><!--&lt;!&ndash; 内存中的最大值 &ndash;&gt;--><!--<property name='maxInMemorySize' value='40960'/>-->

<!--</bean>--> <mvc:default-servlet-handler/></beans>

![图片上传中...]

问题解答

回答1:

报404的话,就是页面、web.xml、spring mvc配置文件、Controller这几个环节出了问题

1、/ 改为/*

2、把Controller代码放出来看看,检查返回的视图是否正确,能否对应上jsp页面

回答2:

<url-pattern>/</url-pattern> 会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀型url

<url-pattern>/</url-pattern> 会匹配所有url:路径型的和后缀型的url(包括/login,.jsp,.js和.html等)

<servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern>

楼主的这段配置没有问题,不需要/ 改为/* ,楼上不要误导。

回答3:

请贴个controller的@RequestMapping配置。

标签: java
相关文章: