您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)什么是springmvc,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
springmvc的工作原理是:1、瀏覽器發(fā)送請(qǐng)求給DispatcherServlet;2、根據(jù)請(qǐng)求找到對(duì)應(yīng)的Controller;3、返回ModelAndView;4、找到對(duì)應(yīng)的視圖對(duì)象View并渲染;5、返回到瀏覽器。
SpringMVC工作原理:
下面我們來看一下代碼實(shí)例:
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> <!-- 項(xiàng)目啟動(dòng)時(shí),就加載并實(shí)例化 --> <load-on-startup>1</load-on-startup> </servlet> <!-- 攔截所有不包括jsp的請(qǐng)求 --> <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注解驅(qū)動(dòng) --> <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",當(dāng)viewname="test"時(shí),跳轉(zhuǎn)到/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; } }
實(shí)體類
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>婚姻狀態(tài)</th> <th>興趣愛好</th> </tr> <!-- 遍歷后臺(tái)傳遞的集合數(shù)據(jù) --> <c:forEach items="${users}" var="user"> <tr> <td>${user.username}</td> <td>${user.age}</td> <td>${user.income}</td> <!-- 判婚姻狀態(tài) --> <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} <!-- 如果不是最后一個(gè)愛好,則加上逗號(hào),否則就不加 --> <c:if test="${!status.last}">,</c:if> </c:forEach> </td> </tr> </c:forEach> </table> </body> </html>
關(guān)于什么是springmvc就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。