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>SpringMVCdisplay-name><servlet><servlet-name>SpringMVCservlet-name><servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class><load-on-startup>1load-on-startup>servlet><servlet-mapping><servlet-name>SpringMVCservlet-name><url-pattern>*.dourl-pattern>servlet-mapping><welcome-file-list><welcome-file>index.jspwelcome-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.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <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> <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{@Overridepublic 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 heretitle> 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.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <mvc:annotation-driven/><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/"/><property name="suffix" value=".jsp"/>bean> <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><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
转载于:https://www.cnblogs.com/wangchuanfu/p/5895107.html
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
