java - SpringMVC 一直提示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' xmlns:web='http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd' 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>SalamanderBBS</display-name> <!-- 起始歡迎界面 --> <welcome-file-list><welcome-file /> </welcome-file-list> <!-- 讀取spring配置文件 --> <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mybatis.xml</param-value> </context-param> <!-- 設計路徑變量值 --> <context-param><param-name>webAppRootKey</param-name><param-value>springmvc.root</param-value> </context-param> <!-- Spring字符集過濾器 --> <filter><filter-name>SpringEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value></init-param><init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value></init-param> </filter> <filter-mapping><filter-name>SpringEncodingFilter</filter-name><url-pattern>/*</url-pattern> </filter-mapping> <listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- springMVC核心配置 --> <servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param> <param-name>contextConfigLocation</param-name> <!--spingMVC的配置路徑 --> <param-value>classpath:spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup> </servlet> <!-- 攔截設置 --> <servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern> </servlet-mapping> <!-- 錯誤跳轉頁面 --> <error-page><!-- 路徑不正確 --><error-code>404</error-code><location>/WEB-INF/view/errpage/404.jsp</location> </error-page> <error-page><!-- 沒有訪問權限,訪問被禁止 --><error-code>405</error-code><location>/WEB-INF/view/errpage/405.jsp</location> </error-page> <error-page><!-- 內部錯誤 --><error-code>500</error-code><location>/WEB-INF/view/errpage/500.jsp</location> </error-page></web-app>
spring-batis.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' xmlns:tx='http://www.springframework.org/schema/tx' xmlns:aop='http://www.springframework.org/schema/aop' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsd'> <!-- 自動掃描 --> <context:component-scan base-package='com.nazi.bbs' /> <!-- 引入配置文件 --> <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='${url}' /><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> <bean class='org.springframework.jdbc.datasource.DataSourceTransactionManager'><property name='dataSource' ref='dataSource' /> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean class='org.mybatis.spring.SqlSessionFactoryBean'><property name='dataSource' ref='dataSource' /><!-- 自動掃描mapping.xml文件 --><property name='mapperLocations' value='classpath:mapper/*.xml'/> </bean> <!-- DAO接口所在包名,Spring會自動查找其下的類 --> <bean class='org.mybatis.spring.mapper.MapperScannerConfigurer'><property name='basePackage' value='com.ymf.whoisquery.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: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-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsd'> <mvc:annotation-driven /> <!-- 自動掃描該包,使SpringMVC認為包下用了@controller注解的類是控制器 --> <context:component-scan base-package='com.nazi.bbs.controller'/> <!--避免IE執行AJAX時,返回JSON出現下載文件 --> <bean class='org.springframework.http.converter.json.MappingJacksonHttpMessageConverter'><property name='supportedMediaTypes'> <list><value>text/html;charset=UTF-8</value> </list></property> </bean> <!-- 定義跳轉的文件的前后綴 ,視圖模式配置--> <bean class='org.springframework.web.servlet.view.InternalResourceViewResolver'><property name='prefix' value='/WEB-INF/view/'/><property name='suffix' value='.jsp'/> </bean></beans>
問題解答
回答1:貼 spring 的 xml 文件上來看看
回答2:index都404?那要看一下部署的容器下面的war包是否存在,以及里面的文件是否存在。
回答3:如果jsp頁面打不開,說明項目沒有部署如果jsp打得開,controller打不開,就在controller中寫個構造函數,看項目啟動的時候有沒有調用,如果沒有調用,要么是你掃包的名字寫錯了,要么就是沒有編譯成功,部署的war包里沒這個class文件
相關文章:
1. node.js - nodejs中把熱request保存下來,使用JSON.stringify(req)報錯,請問怎么解決?2. Angular.js 無法設置Authorization頭,該怎么解決?3. python - angular route 與 django urls 沖突怎么解決?4. javascript - web移動端 ? 版權這個字在安卓上默認藍色顏色 怎么解決?5. javascript - ios返回不執行js怎么解決?6. 我把所有的css代碼放在一個文件夾之后,邊框就變成這個樣子了,請問怎么解決?7. c++ - 請問MySQL_Connection::isReadOnly 怎么解決?8. mysql根據輸入的字段數據獲取到ID怎么解決??9. Mac下phpinfo和php -m里的擴展不一致怎么解決?10. window下mysql中文亂碼怎么解決??
