<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
core, context, web以及webmvc
再來先確認springmvc與web.xml 還有jsp的目錄結構
接著先看一下web.xml的內容 加上
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/springmvc-servlet.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>
注意url-pattern 為 /
此外若有編碼問題則請參照另一篇: 解決Spring mvc裡JSP亂碼問題
springmvc-servlet.xml 裡的內容
<mvc:default-servlet-handler/>
<context:component-scan base-package="controller" />
<mvc:annotation-driven />
<!-- 解析頁面的規則 -->
<bean id="jspviewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 告訴Spring MVC網頁的資料夾 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
因為我大部分使用annotation
因此加上 <mvc:annotation-driven /> 以及欲掃描的package
因此加上 <mvc:annotation-driven /> 以及欲掃描的package
<context:component-scan base-package="controller" />