SpringMVC入门 ( 一 )

先简单搭建一下环境在讲解执行流程

  搭建过程步骤如下:

    1、新建一个JavaWeb项目

    2、导入SpringMVC支持jar包文件加上log4j日志和数据库驱动包总共16个 Spring源码文件中可以找到相应的jar资源

    

3、配置springMVC的配置文件

  

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	<!-- 配置controller扫描包 -->
	<context:component-scan base-package="org.bdqn.demo" />

</beans>

 4、配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>springmvc-first</display-name>
  <servlet>
    <servlet-name>springmvc-first</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc-first</servlet-name>
    <!--匹配拦截的请求  -->
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

  5、编写控制层,使用注解申明

     1、在类上添加@Controller注解,把Controller交由Spring管理

     2、在方法上面添加@RequestMapping注解,里面指定请求的url。其中“.action”可以加也可以不加。

@Controller
public class StudentController {
	@RequestMapping("all.action")
	public ModelAndView ItemList() {
		System.out.println("---------------");
		ModelAndView   modelAndView=new ModelAndView();
		//  添加数据到模型中
		 modelAndView.addObject("login", "admin");
		 // 设置视图的物理地址
		 modelAndView.setViewName("welcome.jsp");
		 return modelAndView;

	}
}

6、启动web服务,进入测试页测试url地址 all.action



进入控制器后 跳回到指定的url地址

欢迎页面如下:


 效果图如下:

 


二:框架结构


架构流程如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值