创建简单的spring-mvc项目
1.第一步:创建项目
new—>Dynamic Web Project
项目创建成功后,展示如图:
2.第二步:导入springmvc的jar包和common-logging.jar
3.第三步:配置web.xml
1 <welcome-file-list> 2 <welcome-file>index.jsp</welcome-file> 3 </welcome-file-list> 4 <!-- 前端控制器配置 --> 5 <servlet> 6 <servlet-name>springmvc</servlet-name> 7 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 8 <!-- 配置spring-mvc核心配置文件 --> 9 <init-param> 10 <param-name>contextConfigLocation</param-name> 11 <param-value>classpath:spring-mvc.xml</param-value> 12 </init-param> 13 </servlet> 14 <!-- 拦截*.do请求 --> 15 <servlet-mapping> 16 <servlet-name>springmvc</servlet-name> 17 <url-pattern>*.do</url-pattern> 18 </servlet-mapping>
4.第四步:在src下添加spring-mvc.xml并配置
1 <!-- 扫描包及其子包下的注解 --> 2 <context:component-scan base-package="com.springmvc"/> 3 <!-- 配置视图解析器 --> 4 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 5 <!-- 前缀 --> 6 <property name="prefix" value="/WEB-INF/jsp/"></property> 7 <!-- 后缀 --> 8 <property name="suffix" value=".jsp"></property> 9 </bean>
5.第五步:在WebContent下创建欢迎页index.jsp
6.第六步:创建controller
1 package com.springmvc; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.ui.Model; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 7 @Controller 8 public class SpringMVCController01 { 9 10 @RequestMapping("/test01") 11 public String test01(Model model){ 12 model.addAttribute("param_id", "01"); 13 model.addAttribute("param_name", "spring-mvc"); 14 return "test"; 15 } 16 }
7.第七步:启动项目
千里之行始于足下!