使用Spring MVC,第一步就是使用Spring提供的前置控制器,即Servlet的实现类DispatcherServlet拦截url:org.springframework.web.servlet.DispatcherServlet;

拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据某某规则分发到目标Controller来处理;

创建一个SpringMVC项目,使用MyEclipse:

1)创建web项目;

2)添加spring相关jar包,不需要格外导入其他的jar包,创建application.xml配置文件;

3)在web.xml上配置DispatcherServlet选项,并指明初始化参数位置为application.xml;

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. <servlet>
  8. <servlet-name>xxx</servlet-name>
  9. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  10. <init-param>
  11. <param-name>contextConfigLocation</param-name>
  12. <param-value>classpath:applicationContext.xml</param-value>
  13. </init-param>
  14. </servlet>
  15. <servlet-mapping>
  16. <servlet-name>xxx</servlet-name>
  17. <url-pattern>*.do</url-pattern>
  18. </servlet-mapping>
  19. <welcome-file-list>
  20. <welcome-file>index.jsp</welcome-file>
  21. </welcome-file-list>
  22. </web-app>

4)在application.xml中配置扫描器context,扫描包中类的所有注解,进行自动装配;配置视图解析器InternalResourceViewResolver;

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-3.1.xsd
  12. ">
  13.  
  14. <!-- 扫描器:扫描包中的所有的注解 进行自动装配 -->
  15. <context:component-scan base-package="com.it"/>
  16. <!-- 视图解析器 -->
  17. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp"/>
  18.  
  19. </beans>

5)创建实体类javabean;

  1. package com.it.entity;
  2.  
  3. /**
  4. * 登陆账户实体类
  5. */
  6. public class Account {
  7. private int id;
  8. private String cardNo;
  9. private String pwd;
  10. private int balance;
  11. private String nickName;
  12.  
  13. public Account() {
  14. super();
  15. }
  16.  
  17. public Account(int id, String cardNo, String pwd, int balance,
  18. String nickName) {
  19. super();
  20. this.id = id;
  21. this.cardNo = cardNo;
  22. this.pwd = pwd;
  23. this.balance = balance;
  24. this.nickName = nickName;
  25. }
  26.  
  27. public int getId() {
  28. return id;
  29. }
  30.  
  31. public void setId(int id) {
  32. this.id = id;
  33. }
  34.  
  35. public String getCardNo() {
  36. return cardNo;
  37. }
  38.  
  39. public void setCardNo(String cardNo) {
  40. this.cardNo = cardNo;
  41. }
  42.  
  43. public String getPwd() {
  44. return pwd;
  45. }
  46.  
  47. public void setPwd(String pwd) {
  48. this.pwd = pwd;
  49. }
  50.  
  51. public int getBalance() {
  52. return balance;
  53. }
  54.  
  55. public void setBalance(int balance) {
  56. this.balance = balance;
  57. }
  58.  
  59. public String getNickName() {
  60. return nickName;
  61. }
  62.  
  63. public void setNickName(String nickName) {
  64. this.nickName = nickName;
  65. }
  66.  
  67. }

6)创建controller类并进行注解配置(@Controller,@RequestMaping);

LoginController.java

  1. package com.it.controller;
  2.  
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.ui.ModelMap;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.RequestParam;
  8. import org.springframework.web.bind.annotation.SessionAttributes;
  9.  
  10. import com.it.entity.Account;
  11.  
  12. /**
  13. * 登陆后端控制器
  14. */
  15. @Controller
  16. // Session范围 将acc存入到session中
  17. @SessionAttributes(value = "acc")
  18. public class LoginController {
  19. // 页面和方法参数一一对应
  20. @RequestMapping(value = "login.do")
  21. // Modelmap参数携带者
  22. public String login(String textName, String textPwd, ModelMap map) {
  23. // 模拟登陆
  24. if ("tom".equals(textName) && "123".equals(textPwd)) {
  25. Account acc = new Account(1001, textName, textPwd, 1000, "游泳的鸟");
  26. map.put("acc", acc);
  27. // 重定向
  28. return "redirect:/list.do";
  29. }
  30. map.put("msg", "登录失败。。。");
  31. return "login";
  32. }
  33.  
  34. // 页面参数和方法参数不一致时,需要requeparam转换
  35. @RequestMapping(value = "login2.do")
  36. public String login2(@RequestParam("name") String textName,@RequestParam("pwd") String textPwd, ModelMap map) {
  37. // 模拟登陆
  38. if ("tom".equals(textName) && "123".equals(textPwd)) {
  39. Account acc = new Account(1001, textName, textPwd, 1000, "游泳的鸟");
  40. map.put("acc", acc);
  41. // 转向list.jsp
  42. return "list";
  43. }
  44. map.put("msg", "登录失败。。。");
  45. // 转向login.jsp
  46. return "login";
  47. }
  48.  
  49. // 使用实体类参数
  50. @RequestMapping(value = "login3.do", method = RequestMethod.POST)
  51. public String login3(Account a, ModelMap map) {
  52. // 模拟登陆
  53. if ("tom".equals(a.getCardNo()) && "123".equals(a.getPwd())) {
  54. Account acc = new Account(1001, a.getCardNo(), a.getPwd(), 1000,
  55. "游泳的鸟");
  56. map.put("acc", acc);
  57. return "list";
  58. }
  59. map.put("msg", "登录失败。。。");
  60. return "login";
  61. }
  62.  
  63. }

ListController.java

  1. package com.it.controller;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.ui.ModelMap;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.SessionAttributes;
  10.  
  11. import com.it.entity.Account;
  12.  
  13. @Controller
  14. @SessionAttributes(value = "ls")
  15. public class ListController {
  16.  
  17. @RequestMapping(value = "list.do")
  18. public String list(ModelMap map) {
  19. List<Account> ls = new ArrayList<Account>();
  20. ls.add(new Account(1001, "tom", "123", 1000, "会飞的鱼"));
  21. ls.add(new Account(1002, "tony", "111", 1500, "会游泳的鸟"));
  22. ls.add(new Account(1003, "jack", "000", 2000, "开飞机的乌龟"));
  23. map.put("ls", ls);
  24. return "list";
  25. }
  26. }

7)创建jsp页面,页面上的数据使用EL表达式来显示;

login.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme() + "://"
  5. + request.getServerName() + ":" + request.getServerPort()
  6. + path + "/";
  7. %>
  8.  
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  10. <html>
  11. <head>
  12. <base href="<%=basePath%>">
  13.  
  14. <title>My JSP 'index.jsp' starting page</title>
  15. <meta http-equiv="pragma" content="no-cache">
  16. <meta http-equiv="cache-control" content="no-cache">
  17. <meta http-equiv="expires" content="0">
  18. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  19. <meta http-equiv="description" content="This is my page">
  20. <!--
  21. <link rel="stylesheet" type="text/css" href="styles.css">
  22. -->
  23. </head>
  24.  
  25. <body>
  26. <form action="login.do" method="post">
  27. <input name="textName" /> <input name="textPwd" /> <input
  28. type="submit" value="登陆...">
  29. </form>
  30. <span style="color: red;">${msg }</span>
  31. </body>
  32. </html>

login2.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme() + "://"
  5. + request.getServerName() + ":" + request.getServerPort()
  6. + path + "/";
  7. %>
  8.  
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  10. <html>
  11. <head>
  12. <base href="<%=basePath%>">
  13.  
  14. <title>My JSP 'index.jsp' starting page</title>
  15. <meta http-equiv="pragma" content="no-cache">
  16. <meta http-equiv="cache-control" content="no-cache">
  17. <meta http-equiv="expires" content="0">
  18. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  19. <meta http-equiv="description" content="This is my page">
  20. <!--
  21. <link rel="stylesheet" type="text/css" href="styles.css">
  22. -->
  23. </head>
  24.  
  25. <body>
  26. <form action="login2.do" method="post">
  27. <input name="name" /> <input name="pwd" /> <input
  28. type="submit" value="登陆...">
  29. </form>
  30. <span style="color: red;">${msg }</span>
  31. </body>
  32. </html>

login3.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme() + "://"
  5. + request.getServerName() + ":" + request.getServerPort()
  6. + path + "/";
  7. %>
  8.  
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  10. <html>
  11. <head>
  12. <base href="<%=basePath%>">
  13.  
  14. <title>My JSP 'index.jsp' starting page</title>
  15. <meta http-equiv="pragma" content="no-cache">
  16. <meta http-equiv="cache-control" content="no-cache">
  17. <meta http-equiv="expires" content="0">
  18. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  19. <meta http-equiv="description" content="This is my page">
  20. <!--
  21. <link rel="stylesheet" type="text/css" href="styles.css">
  22. -->
  23. </head>
  24.  
  25. <body>
  26. <form action="login3.do" method="post">
  27. <input name="cardNo" /> <input name="pwd" /> <input
  28. type="submit" value="登陆...">
  29. </form>
  30. <span style="color: red;">${msg }</span>
  31. </body>
  32. </html>

list.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme() + "://"
  5. + request.getServerName() + ":" + request.getServerPort()
  6. + path + "/";
  7. %>
  8. <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  9.  
  10. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  11. <html>
  12. <head>
  13. <base href="<%=basePath%>">
  14.  
  15. <title>My JSP 'index.jsp' starting page</title>
  16. <meta http-equiv="pragma" content="no-cache">
  17. <meta http-equiv="cache-control" content="no-cache">
  18. <meta http-equiv="expires" content="0">
  19. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  20. <meta http-equiv="description" content="This is my page">
  21. <!--
  22. <link rel="stylesheet" type="text/css" href="styles.css">
  23. -->
  24. </head>
  25.  
  26. <body>
  27. <center>
  28. <h1>欢迎你,${sessionScope.acc.nickName }</h1>
  29. </center>
  30. <table align="center" border="1px;" width="40%">
  31. <tr align="center">
  32. <td>序号</td>
  33. <td>账号</td>
  34. <td>密码</td>
  35. <td>余额</td>
  36. <td>昵称</td>
  37. <td>操作</td>
  38. </tr>
  39. <c:forEach var="acc" items="${ls }">
  40. <tr align="center">
  41. <td>${acc.id }</td>
  42. <td>${acc.cardNo }</td>
  43. <td>${acc.pwd }</td>
  44. <td>${acc.balance }</td>
  45. <td>${acc.nickName }</td>
  46. <td>
  47. <a href="#">删除</a>
  48. <a href="#">修改</a>
  49. </td>
  50. </tr>
  51. </c:forEach>
  52. </table>
  53. </body>
  54. </html>

8)配置服务器即可测试。

SpringMVC基本使用步骤的更多相关文章

  1. springMVC的详细步骤配置

    使用springMVC也可以代替struts2,当然只是代替业务分发的功能,struts2的一些其他功能它是没有的,不然要struts2有什么用. 下面我用springMVC代替struts2去整合h ...

  2. springMvc(初识+操作步骤)

    1.导入包2.配置web.xml <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:x ...

  3. 黑马学习SpringMVC 基本开发步骤

  4. SpringMVC学习记录2

    废话 最近在看SpringMVC...里面东西好多...反正东看一点西看一点吧... 分享一下最近的一些心得..是关于DispatcherServlet的 DispatcherServlet与Cont ...

  5. 2. SpringMVC 上传文件操作

    1.创建java web项目:SpringMVCUploadDownFile 2.在项目的WebRoot下的WEB-INF的lib包下添加如下jar文件 com.springsource.com.mc ...

  6. SpringMVC配置实例

    一.SpringMVC概述 MVCII模式实现的框架技术 Model--业务模型(Biz,Dao...) View--jsp及相关的jquery框架技术(easyui) Contraller--Dis ...

  7. 转:SpringMVC浅谈

    因为项目文案需要,于是乎翻阅spring相关资料.顿觉该篇不错详尽易懂,特转载之. 转载出处: http://blog.csdn.net/gane_cheng/article/details/5278 ...

  8. 创建一个可用的简单的SpringMVC项目,图文并茂

    转载麻烦注明下来源:http://www.cnblogs.com/silentdoer/articles/7134332.html,谢谢. 最近在自学SpringMVC,百度了很多资料都是比较老的,而 ...

  9. SpringMVC框架01——使用IDEA搭建SpringMVC环境

    1.Spring MVC 入门 1.1.Spring MVC 简介 把Web应用程序分为三层,分别是: 控制器(Controller):负责接收并处理请求,响应客户端: 模型(Model):模型数据, ...

随机推荐

  1. iphone对fixed模态框支持不太好,弹出窗口中滚动点击穿透的bug

    iphone对fixed展现层中存在滚动内容支持非常不好, 尤其是背景页面产生滚动以后,输入控件就找不到了, 取消冒泡也不行,最后是这么解决的,可以参考 <style> .modeldiv ...

  2. java学习——内部类(一)

    内部类 把一个类放在另一个类中定义,这个定义在其他内部的类被称为内部类,包含内部类 的类被成为外部类,Java从JDK1.1开始引入了内部类的定义. 内部类的作用: 内部类提供了更好的封装,可以把内部 ...

  3. Java方法的多态

    方法的多态 重载.方法名相同,其他可以不同 重写.父类子类的方法名相同.访问权限不能更小 重写: 继承了父类 父类方法不能满足 子类.父类这个方法的方法名相同 参数相同 返回类型相同 访问权限不能更小 ...

  4. Linux之程序的开始和结束

    1.main函数由谁来调用 (1).编译链接时的引导代码. 操作系统下的应用程序其实是在main函数执行前也需要先执行一段引导代码才能去执行main函数,我们写应用程序时不用考虑引导代码的问题,编译链 ...

  5. 微信小程序生成海报保存图片到相册小测试

    test.wxml <canvas style="width:{{imageWidth}}px;height:{{imageHeight}}px;" canvas-id=&q ...

  6. psi 函数计算

    scipy.special.psi odps中不支持 scipy.special.psi,需要改写 基于 chebyshev_polynomial https://people.sc.fsu.edu/ ...

  7. 黑科技如何制造人类V2.0?

    黑科技泛指人类尚未成熟但具有巨大潜力的科学技术,智能手机.大数据.扫码支付.电子地图等等都曾属于黑科技范畴,随着时间的推移,它们慢慢成熟,且展现出巨大的能力,影响人类进程,最终黑科技转变成人类伟大的创 ...

  8. tesseract系列(2) -- tesseract的使用

    上文说了怎么编译成库,这次说说怎么使用,先验证下编译出来的结果. 下图是debug生成的文件,里面有个tesseract的应用程序. cmd进入目录下,执行命令:tesseract eurotext. ...

  9. hm nsis edit请求的操作需要提升

    第一次用nsis做安装包,编译运行出现这个问题,解决办法:管理员身份运行即可

  10. Nginx_安全1

    Nginx 安全 nginx隐藏版本号 # 在Nginx的配置文件中进行修改,增加下面这个. server_tokens on; nginx对IP和目录限速 # Nginx可以通过HTTPLimitZ ...