1.使用maven导入spring mvc web 的jar 包 最新语句
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
2.配置web.xml,区别于struts2,springmvc使用servlet分发请求
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 核心类-->
<load-on-startup>1</load-on-startup><!--马上启动-->
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern><!--匹配url-->
</servlet-mapping>
3.需要spring自己的配置,默认会在WEB-INF目录下查找 <servlet-name>-name.xml的配置文件
在servlet配置加入
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring-servlet.xml</param-value>
</init-param>
param-value中可以从根目录自己设置。当然也可以用classpath.用逗号分隔
4.配置spring-servlet.xml
<!-- 映射扫描包 -->
<context:component-scan base-package="com.citiboy.action"/>
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<!-- 视图解释类 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>
<!-- 对静态资源文件的访问 -->
<mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/>
<mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>
<mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/>
5.写action相关即可
@Controller("SingleTestAction")
public class SingleTestAction {
@RequestMapping("/user")
public String execute()
{
System.out.println("123");
return "redirect:index.jsp";
}
}
6.规划url ,开发action