Spring mvc 学习笔记
记录下学习spring-mvc的技术要点,项目结构路径如下:
1. 首先要在web.xml中添加Servlet和filter的配置:
<!-- 转码,防止乱码-->
<filter> <filter-name>encodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring 核心 servlet--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- 指定spring 核心的配置文件--> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
2. spring-servlet.xml的配置如下
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 导入spring容器的bean <import resource="classpath:applicationContext-*.xml" /> --> <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射 --> <mvc:annotation-driven /> <context:annotation-config /> <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean --> <context:component-scan base-package="com.xx.spring.mvc.demo.controller" /> <!-- .do 映射对应名字的jsp--> <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/" p:suffix=".jsp" /> <!--直接转发 将.do请求转发为jsp--> <mvc:view-controller path="/add.do" view-name="add"/> </bean>
3. spring 通过注解方式实现的controller
//表示这是一个controller类 @Controller //表示这个类所有方法都是在/mvc/myMvc/路径下,其中mvc是项目名 @RequestMapping(value = "/myMvc") public class DemoController { /** * 表示对应/mvc/myMvc/zhangsan 这类请求 * @PathVariable("name") String name 表示将uri的变量作为一个参数使用 */ @RequestMapping(value="/{name}") public ModelAndView myMvc(@PathVariable("name") String name, HttpServletRequest request, HttpServletResponse response,ModelMap modelMap){ modelMap.put("name",name); /** * /welcome 通过下面的配置映射到了 /WEB-INF/view/welcome.jsp上 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/" p:suffix=".jsp" /> */ return new ModelAndView("/welcome",modelMap); } /** * * @param girl 前台输入页面控件name属性和po类BeautifulGirl的属性一样对应 * 使用@ModelAttribute命令可以在调用方法前自动封装一个BeautifulGirl的对象girl * @param request * @param response * @param modelMap * @return */ @RequestMapping(value = "/addGirl") public ModelAndView addUser(@ModelAttribute("girl")BeautifulGirl girl, HttpServletRequest request,HttpServletResponse response,ModelMap modelMap){ modelMap.put("girl",girl); return new ModelAndView("/welcomeGirl",modelMap); } }
4. pojo类 BeautifulGirl
public class BeautifulGirl { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
5. add.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; String name = (String)request.getAttribute("name"); %> <html> <body> <form action="<%=basePath%>/myMvc/addGirl.do" method="post"> 姓名:<input type="text" name="name"/><br> 年龄:<input type="text" name="age"/><br> <input type="submit" value="提交" /> </form> </body> </html>
6. welcomeGirl.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="com.jd.spring.mvc.demo.po.BeautifulGirl" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; String name = (String)request.getAttribute("name"); BeautifulGirl beautifulGirl = (BeautifulGirl)request.getAttribute("girl"); %> <html> <body> Hello,美女. 你的名字:<%=beautifulGirl.getName() %> <br> 你的姓名:<%=beautifulGirl.getAge()%> </body> </html>
持续学习、持续收获才能带来持续的满足和快乐!