springmvc入门详解
首先,我们先写一个入门小案例,先熟悉一下springmvc是什么,了解一下springmvc的运行流程,对加强springmvc的深层理解有很大帮助
.第一步,创建一个maven项目:
<?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" id="MyWebApp" version="2.5"> <display-name>SpringMVC</display-name> <!-- SpringMVC的入口 --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> <!-- 默认加载配置文件: /WEB-INF/{servlet-name}-servlet.xml --> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- 可选配置: *.do、*.action / /xxx/* 不可以: /* --> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
<?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" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 定义handlerMapping --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- 定义适配器 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!-- 定义视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/views/"/> <!-- 后缀 --> <property name="suffix" value=".jsp"/> </bean> <!-- 定义Handler对象 --> <bean name="/hello.do" class="com.j1.springmvc.controller.HelloController"/> </beans>
package com.j1.springmvc.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class HelloController implements Controller{ @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mv =new ModelAndView(); mv.setViewName("hellos"); mv.addObject("msg","恭喜你,小逼崽子,第一个SpringMvc程序成功了"); return mv; } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> </head> <body> <h1>${msg}</h1> </body> </html>
在浏览器输入:http://localhost:8080/myspring-demo/hello.do
效果如图所示:
至此一个入门记得springmvc就写好了.
使用注解
package com.j1.springmvc.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @RequestMapping("/hello2") @Controller public class Hello2Controller { @RequestMapping("/show") public ModelAndView show(){ ModelAndView mv = new ModelAndView(); mv.setViewName("hello"); mv.addObject("msg", "呀呀呀,恭喜你,小逼崽子,我的第一个注解Controller测试成功!"); return mv; } }
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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 定义handlerMapping --> <!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> --> <!-- 定义适配器 --> <!-- <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> --> <!-- 注解驱动 --> <mvc:annotation-driven/> <!-- 定义视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/views/"/> <!-- 后缀 --> <property name="suffix" value=".jsp"/> </bean> <!-- 定义Handler对象 --> <!-- <bean name="/hello.do" class="com.j1.springmvc.controller.HelloController"/> --> <!-- 配置扫描器,使得@Controller注解生效 --> <context:component-scan base-package="com.j1.springmvc.controller"/> <!-- 定义文件上传解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设定默认编码 --> <property name="defaultEncoding" value="UTF-8"></property> <!-- 设定文件上传的最大值5MB,5*1024*1024 --> <property name="maxUploadSize" value="5242880"></property> </bean> </beans>
在浏览中输入:http://localhost:8080/myspring-demo/hello2/show.do
http://jinnianshilongnian.iteye.com/blog/1594806
http://blog.csdn.net/kxd_ysheng/article/details/24471683
http://www.cnblogs.com/xujian2014/p/5235145.html
http://www.cnblogs.com/liusk/p/4195256.html
http://www.cnblogs.com/fangjian0423/tag/springmvc/
http://www.cnblogs.com/superjt/p/3309255.html
http://blog.csdn.net/kxd_ysheng/article/details/24690689
无为而治