先說一下我的錯誤吧
在配置前端控制器的時候,有一個標籤寫錯了我
使用exclude-filter 類似於把controller註解加入了黑名單 ,使用exclude
表示一般分開加載的時候在加載spring配置文件的時候只掃描@service和@Reposity這些類
修改之後
測試結果:
下面對應一下我的配置:可以參考查看一下
1.目錄文件
2.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>Archetype Created Web Application</display-name>
<!-- 配置DisppatcherServlet控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置一個DispatcherServlet初始化參數:配置SpringMVC配置文件的位置和名稱-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/springmvc.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>
<!--配置springmvc編碼過濾器-->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3.springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 開啓註解掃描 只掃描Controller註解 -->
<context:component-scan base-package="com.bhlc">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置的視圖解析器對象-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--設置過濾靜態資源不過濾-->
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/images/**" location="/images"/>
<mvc:resources mapping="/js/**" location="/js/"/>
<!--開啓SpringMVC註解的支持-->
<mvc:annotation-driven/>
</beans>
4.前端控制類
//AccountController類
@RequestMapping("/account")
@Controller
public class AccountController {
@RequestMapping("/findAll")
public String findAll(){
System.out.println("表現層執行。。。");
return "out";
}
}
5.然後就是新建兩個jsp文件進行頁面跳轉了
(注意,如果檢查配置文件沒錯的話,看看是不是設置的路徑有問題!一般就是這些問題)