SpringMVC框架
我们在基于Spring框架进行web开发时,通常会有dao层(数据库操作),service层(业务逻辑操作,比如判断是否在redis,memcached等缓存中查找数据,何时调用dao层),web层(接收前端用户的请求,调用service层,向前端响应数据与视图)。其中,Spring框架为service层做了较好的支持,mybatis、hibernate等框架为dao层做了较好的支持,Spring-MVC、Struts2等框架为web层做了较好的支持。通常来说,基于Spring框架开发的web项目有SSM(Spring,Spring-MVC,mybatis)体系,SSH(Spring,Spring-MVC,hibernate)体系等。我在此处通过代码演示下Spring-MVC是如何运行的。先看Spring-MVC框架的运行图:
首先,我们要在web项目中添加Spring-MVC的jar包依赖,在此基础上开发项目。本项目案例文件包结构如下所示:
接下来,配置web.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!--注册过滤器,避免中文乱码--> <filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--注册前端控制器--> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> </web-app>
再配置springmvc-config.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--配置页面控制器/处理器,采用注解方式--> <context:component-scan base-package="com.itszt.controller"></context:component-scan> <!--寻址与控制器处理的注解支持--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> <!--配置视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
后台Java代码如下:
package com.itszt.domain; /** * 实体类,狗的名字,颜色 */ public class Dog { private String dogname; private String dogcolor; public String getDogname() { return dogname; } @Override public String toString() { return "Dog{" + "dogname='" + dogname + '\'' + ", dogcolor='" + dogcolor + '\'' + '}'; } public void setDogname(String dogname) { this.dogname = dogname; } public String getDogcolor() { return dogcolor; } public void setDogcolor(String dogcolor) { this.dogcolor = dogcolor; } } ---------------------------------------------- package com.itszt.domain; import java.util.Arrays; /** * 实体类,用户的姓名,年龄,爱好,宠物狗 */ public class User { private String username; private Integer userage; private String[] userhbs; private Dog dog; public String getUsername() { return username; } @Override public String toString() { return "User{" + "username='" + username + '\'' + ", userage=" + userage + ", userhbs=" + Arrays.toString(userhbs) + ", dog=" + dog + '}'; } public void setUsername(String username) { this.username = username; } public Integer getUserage() { return userage; } public void setUserage(Integer userage) { this.userage = userage; } public String[] getUserhbs() { return userhbs; } public void setUserhbs(String[] userhbs) { this.userhbs = userhbs; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } } --------------------------------------------------------- package com.itszt.controller; import com.itszt.domain.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; /** * 控制器 */ @Controller @RequestMapping("/orgCenter") public class OrgController { @RequestMapping("/add") public String add(User user, HttpServletRequest request, HttpSession session){ System.out.println("OrgController.add"); System.out.println("接收参数User="+user); request.setAttribute("user",user); StringBuffer sbf=new StringBuffer(); for(String s:user.getUserhbs()){ sbf.append(s+","); } String userhbs=sbf.toString(); request.setAttribute("userhbs",userhbs.substring(0,userhbs.length()-1)); request.setAttribute("dog",user.getDog()); return "result"; } }
前端index.jsp页面代码如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>演示springmvc实现过程</title> </head> <body> <form action="/orgCenter/add.html" method="post"> <h3> 用户姓名:<input type="text" name="username" value="张三"/> </h3> <h3> 用户年龄:<input type="number" name="userage" value="15"/> </h3> <h3> 用户爱好:<input type="checkbox" name="userhbs" value="climb"/>爬山 <input type="checkbox" name="userhbs" value="swim"/>游泳 <input type="checkbox" name="userhbs" value="basketball"/>篮球 <input type="checkbox" name="userhbs" value="football"/>足球 </h3> <h3> 宠物狗: 名字:<input type="text" name="dog.dogname" value="旺旺"/> 毛色:<input type="text" name="dog.dogcolor" value="黄色"/> </h3> <h3> <input type="submit" value="提交"> </h3> </form> </body> </html>
服务器端的OrgController响应页面如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>返回结果</title> </head> <body> 我是result.jsp页面<br> 返回结果是:<br> 您发来的用户姓名:${user.username}<br> 用户年龄:${user.userage}<br> 用户爱好:${userhbs}<br> 宠物狗:<br> 名字:${dog.dogname}<br> 毛色:${dog.dogcolor}<br> </body> </html>