由于pom.xml中没配置web资源,导致controller跳转页面失败【404】!!
关键配置: 在build->plugins加 :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<webXml>web\WEB-INF\web.xml</webXml>
<warSourceDirectory>web</warSourceDirectory>
</configuration>
</plugin>
这是最大的坑,找了很久的问题。主要是配置web.xml和jsp资源位置
其次,是配置接口转换数据问题,在servletXml文件中
关键配置:
<mvc:annotation-driven>
<mvc:message-converters>
<!--配置返回的类型转换-->
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
总共的配置有 1.pom 2.web.xml 3.servlet.xml
在web.xml中要引用servlet.xml
servlet.xml核心配置:
<context:component-scan base-package="com.hjm.controller"></context:component-scan> <context:annotation-config /> <mvc:default-servlet-handler/> <mvc:annotation-driven> <mvc:message-converters> <!--配置返回的类型转换--> <bean class="org.springframework.http.converter.StringHttpMessageConverter" /> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /> </mvc:message-converters> </mvc:annotation-driven> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"></property> <property name="suffix" value=".jsp"></property> </bean>
pom.xml注意的配置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <webXml>web\WEB-INF\web.xml</webXml> <warSourceDirectory>web</warSourceDirectory> </configuration> </plugin>
web.xml核心配置:
<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*:application.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>