springmvc工作原理:
(推荐学习:java入门程序)
1、浏览器发送指定的请求都会交给dispatcherservlet,他会委托其他模块进行真正的业务和数据处理;
2、dispatcherservlet会查找到handlemapping,根据浏览器的请求找到对应的controller,并将请求交给目标controller;
3、目标controller处理完业务后,返回一个modelandview给dispatcherservlet;
4、dispatcherservlet通过viewresolver视图解析器找到对应的视图对象view;
4、视图对象view负责渲染,并返回到浏览器。
(视频教程推荐:java视频教程)
下面我们来看一下代码实例:
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" version="2.5"> <display-name>springmvc</display-name> <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> <!-- spring入口 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <!-- 项目启动时,就加载并实例化 --> <load-on-startup>1</load-on-startup> </servlet> <!-- 拦截所有不包括jsp的请求 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping></web-app>
springmvc-servlet.xml
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- springmvc注解驱动 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 开启注解扫描 --> <context:component-scan base-package="cn.itcast.controller"></context:component-scan> <!-- 配置试图解析器 prefix:指定试图所在目录 suffix:指定视图的后缀名 例如:prifex="/web-inf/jsp/",suffix=".jsp",当viewname="test"时,跳转到/web-inf/jsp/test.jsp页面 --> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean></beans>
usercontroll类
@controller@requestmapping("user")public class usercontroller { @requestmapping("findallusers") public modelandview findallusers() { modelandview mv = new modelandview(); arraylist<user> users = new arraylist<user>(); for (int i = 0; i < 5; i++) { user user = new user(); user.setusername("zs" + i); user.setage(20 + i); user.setincome(16000.0+i*100); user.setismarry(false); user.sethobby(new string[] { "篮球"+i, "足球"+i }); users.add(user); } mv.addobject("users", users); mv.setviewname("users"); return mv; }}
实体类
public class user implements serializable { /** * */ private static final long serialversionuid = 1l; private string username; private integer age; private boolean ismarry; private double income; private string[] hobby; public string getusername() { return username; } public void setusername(string username) { this.username = username; } public integer getage() { return age; } public void setage(integer age) { this.age = age; } public boolean getismarry() { return ismarry; } public void setismarry(boolean ismarry) { this.ismarry = ismarry; } public double getincome() { return income; } public void setincome(double income) { this.income = income; } public string[] gethobby() { return hobby; } public void sethobby(string[] hobby) { this.hobby = hobby; } @override public string tostring() { return "user [username=" + username + ", age=" + age + ", ismarry=" + ismarry + ", income=" + income + ", hobby=" + arrays.tostring(hobby) + "]"; }}
jsp页面
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"><html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"><title>insert title here</title><link rel="stylesheet" type="text/css" href="/css/user.css" /></head><body> <table id="customers"> <tr> <th>用户名</th> <th>年龄</th> <th>收入</th> <th>婚姻状态</th> <th>兴趣爱好</th> </tr> <!-- 遍历后台传递的集合数据 --> <c:foreach items="${users}" var="user"> <tr> <td>${user.username}</td> <td>${user.age}</td> <td>${user.income}</td> <!-- 判婚姻状态 --> <td><c:choose> <c:when test="${user.ismarry}">已婚</c:when> <c:otherwise>未婚</c:otherwise> </c:choose> </td> <td> <!-- 再次遍历用户爱好 --> <c:foreach items="${user.hobby}" var="hobby" varstatus="status"> ${hobby} <!-- 如果不是最后一个爱好,则加上逗号,否则就不加 --> <c:if test="${!status.last}">,</c:if> </c:foreach> </td> </tr> </c:foreach> </table></body></html>
以上就是springmvc的工作原理是什么的详细内容。
