SpringMVC基本使用步骤
使用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;
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <servlet>
- <servlet-name>xxx</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>xxx</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
4)在application.xml中配置扫描器context,扫描包中类的所有注解,进行自动装配;配置视图解析器InternalResourceViewResolver;
- <?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"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.1.xsd
- ">
- <!-- 扫描器:扫描包中的所有的注解 进行自动装配 -->
- <context:component-scan base-package="com.it"/>
- <!-- 视图解析器 -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp"/>
- </beans>
5)创建实体类javabean;
- package com.it.entity;
- /**
- * 登陆账户实体类
- */
- public class Account {
- private int id;
- private String cardNo;
- private String pwd;
- private int balance;
- private String nickName;
- public Account() {
- super();
- }
- public Account(int id, String cardNo, String pwd, int balance,
- String nickName) {
- super();
- this.id = id;
- this.cardNo = cardNo;
- this.pwd = pwd;
- this.balance = balance;
- this.nickName = nickName;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getCardNo() {
- return cardNo;
- }
- public void setCardNo(String cardNo) {
- this.cardNo = cardNo;
- }
- public String getPwd() {
- return pwd;
- }
- public void setPwd(String pwd) {
- this.pwd = pwd;
- }
- public int getBalance() {
- return balance;
- }
- public void setBalance(int balance) {
- this.balance = balance;
- }
- public String getNickName() {
- return nickName;
- }
- public void setNickName(String nickName) {
- this.nickName = nickName;
- }
- }
6)创建controller类并进行注解配置(@Controller,@RequestMaping);
LoginController.java
- package com.it.controller;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.ModelMap;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.SessionAttributes;
- import com.it.entity.Account;
- /**
- * 登陆后端控制器
- */
- @Controller
- // Session范围 将acc存入到session中
- @SessionAttributes(value = "acc")
- public class LoginController {
- // 页面和方法参数一一对应
- @RequestMapping(value = "login.do")
- // Modelmap参数携带者
- public String login(String textName, String textPwd, ModelMap map) {
- // 模拟登陆
- if ("tom".equals(textName) && "123".equals(textPwd)) {
- Account acc = new Account(1001, textName, textPwd, 1000, "游泳的鸟");
- map.put("acc", acc);
- // 重定向
- return "redirect:/list.do";
- }
- map.put("msg", "登录失败。。。");
- return "login";
- }
- // 页面参数和方法参数不一致时,需要requeparam转换
- @RequestMapping(value = "login2.do")
- public String login2(@RequestParam("name") String textName,@RequestParam("pwd") String textPwd, ModelMap map) {
- // 模拟登陆
- if ("tom".equals(textName) && "123".equals(textPwd)) {
- Account acc = new Account(1001, textName, textPwd, 1000, "游泳的鸟");
- map.put("acc", acc);
- // 转向list.jsp
- return "list";
- }
- map.put("msg", "登录失败。。。");
- // 转向login.jsp
- return "login";
- }
- // 使用实体类参数
- @RequestMapping(value = "login3.do", method = RequestMethod.POST)
- public String login3(Account a, ModelMap map) {
- // 模拟登陆
- if ("tom".equals(a.getCardNo()) && "123".equals(a.getPwd())) {
- Account acc = new Account(1001, a.getCardNo(), a.getPwd(), 1000,
- "游泳的鸟");
- map.put("acc", acc);
- return "list";
- }
- map.put("msg", "登录失败。。。");
- return "login";
- }
- }
ListController.java
- package com.it.controller;
- import java.util.ArrayList;
- import java.util.List;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.ModelMap;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.SessionAttributes;
- import com.it.entity.Account;
- @Controller
- @SessionAttributes(value = "ls")
- public class ListController {
- @RequestMapping(value = "list.do")
- public String list(ModelMap map) {
- List<Account> ls = new ArrayList<Account>();
- ls.add(new Account(1001, "tom", "123", 1000, "会飞的鱼"));
- ls.add(new Account(1002, "tony", "111", 1500, "会游泳的鸟"));
- ls.add(new Account(1003, "jack", "000", 2000, "开飞机的乌龟"));
- map.put("ls", ls);
- return "list";
- }
- }
7)创建jsp页面,页面上的数据使用EL表达式来显示;
login.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'index.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- <form action="login.do" method="post">
- <input name="textName" /> <input name="textPwd" /> <input
- type="submit" value="登陆...">
- </form>
- <span style="color: red;">${msg }</span>
- </body>
- </html>
login2.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'index.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- <form action="login2.do" method="post">
- <input name="name" /> <input name="pwd" /> <input
- type="submit" value="登陆...">
- </form>
- <span style="color: red;">${msg }</span>
- </body>
- </html>
login3.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'index.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- <form action="login3.do" method="post">
- <input name="cardNo" /> <input name="pwd" /> <input
- type="submit" value="登陆...">
- </form>
- <span style="color: red;">${msg }</span>
- </body>
- </html>
list.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'index.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- <center>
- <h1>欢迎你,${sessionScope.acc.nickName }</h1>
- </center>
- <table align="center" border="1px;" width="40%">
- <tr align="center">
- <td>序号</td>
- <td>账号</td>
- <td>密码</td>
- <td>余额</td>
- <td>昵称</td>
- <td>操作</td>
- </tr>
- <c:forEach var="acc" items="${ls }">
- <tr align="center">
- <td>${acc.id }</td>
- <td>${acc.cardNo }</td>
- <td>${acc.pwd }</td>
- <td>${acc.balance }</td>
- <td>${acc.nickName }</td>
- <td>
- <a href="#">删除</a>
- <a href="#">修改</a>
- </td>
- </tr>
- </c:forEach>
- </table>
- </body>
- </html>
8)配置服务器即可测试。
SpringMVC基本使用步骤的更多相关文章
- springMVC的详细步骤配置
使用springMVC也可以代替struts2,当然只是代替业务分发的功能,struts2的一些其他功能它是没有的,不然要struts2有什么用. 下面我用springMVC代替struts2去整合h ...
- springMvc(初识+操作步骤)
1.导入包2.配置web.xml <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:x ...
- 黑马学习SpringMVC 基本开发步骤
- SpringMVC学习记录2
废话 最近在看SpringMVC...里面东西好多...反正东看一点西看一点吧... 分享一下最近的一些心得..是关于DispatcherServlet的 DispatcherServlet与Cont ...
- 2. SpringMVC 上传文件操作
1.创建java web项目:SpringMVCUploadDownFile 2.在项目的WebRoot下的WEB-INF的lib包下添加如下jar文件 com.springsource.com.mc ...
- SpringMVC配置实例
一.SpringMVC概述 MVCII模式实现的框架技术 Model--业务模型(Biz,Dao...) View--jsp及相关的jquery框架技术(easyui) Contraller--Dis ...
- 转:SpringMVC浅谈
因为项目文案需要,于是乎翻阅spring相关资料.顿觉该篇不错详尽易懂,特转载之. 转载出处: http://blog.csdn.net/gane_cheng/article/details/5278 ...
- 创建一个可用的简单的SpringMVC项目,图文并茂
转载麻烦注明下来源:http://www.cnblogs.com/silentdoer/articles/7134332.html,谢谢. 最近在自学SpringMVC,百度了很多资料都是比较老的,而 ...
- SpringMVC框架01——使用IDEA搭建SpringMVC环境
1.Spring MVC 入门 1.1.Spring MVC 简介 把Web应用程序分为三层,分别是: 控制器(Controller):负责接收并处理请求,响应客户端: 模型(Model):模型数据, ...
随机推荐
- iphone对fixed模态框支持不太好,弹出窗口中滚动点击穿透的bug
iphone对fixed展现层中存在滚动内容支持非常不好, 尤其是背景页面产生滚动以后,输入控件就找不到了, 取消冒泡也不行,最后是这么解决的,可以参考 <style> .modeldiv ...
- java学习——内部类(一)
内部类 把一个类放在另一个类中定义,这个定义在其他内部的类被称为内部类,包含内部类 的类被成为外部类,Java从JDK1.1开始引入了内部类的定义. 内部类的作用: 内部类提供了更好的封装,可以把内部 ...
- Java方法的多态
方法的多态 重载.方法名相同,其他可以不同 重写.父类子类的方法名相同.访问权限不能更小 重写: 继承了父类 父类方法不能满足 子类.父类这个方法的方法名相同 参数相同 返回类型相同 访问权限不能更小 ...
- Linux之程序的开始和结束
1.main函数由谁来调用 (1).编译链接时的引导代码. 操作系统下的应用程序其实是在main函数执行前也需要先执行一段引导代码才能去执行main函数,我们写应用程序时不用考虑引导代码的问题,编译链 ...
- 微信小程序生成海报保存图片到相册小测试
test.wxml <canvas style="width:{{imageWidth}}px;height:{{imageHeight}}px;" canvas-id=&q ...
- psi 函数计算
scipy.special.psi odps中不支持 scipy.special.psi,需要改写 基于 chebyshev_polynomial https://people.sc.fsu.edu/ ...
- 黑科技如何制造人类V2.0?
黑科技泛指人类尚未成熟但具有巨大潜力的科学技术,智能手机.大数据.扫码支付.电子地图等等都曾属于黑科技范畴,随着时间的推移,它们慢慢成熟,且展现出巨大的能力,影响人类进程,最终黑科技转变成人类伟大的创 ...
- tesseract系列(2) -- tesseract的使用
上文说了怎么编译成库,这次说说怎么使用,先验证下编译出来的结果. 下图是debug生成的文件,里面有个tesseract的应用程序. cmd进入目录下,执行命令:tesseract eurotext. ...
- hm nsis edit请求的操作需要提升
第一次用nsis做安装包,编译运行出现这个问题,解决办法:管理员身份运行即可
- Nginx_安全1
Nginx 安全 nginx隐藏版本号 # 在Nginx的配置文件中进行修改,增加下面这个. server_tokens on; nginx对IP和目录限速 # Nginx可以通过HTTPLimitZ ...