首页 > 代码库 > 孔浩Spring mvc 笔记
孔浩Spring mvc 笔记
Spring mvc 配置:
Web.xml:
<?xml version="1.0"encoding="UTF-8"?> <web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"> <servlet> <servlet-name>hello</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>CharacterFilter</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>CharacterFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
Hello-servlet.xml
(名称必须与web.xm文件中的servlet-name相同,放到web-inf下)
<?xml version="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsd"> <context:component-scanbase-package="zttc.itat.controller"/> <mvc:annotation-driven/> <!--将静态文件指定到某个特殊的文件夹中统一处理 --> <mvc:resourceslocation="/resources/" mapping="/resources/**"/> <beanname="/welcome.html"class="zttc.itat.controller.WelcomeController"></bean> <beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"> <propertyname="viewClass"value=http://www.mamicode.com/"org.springframework.web.servlet.view.JstlView"/>>UserController .java
@Controller @RequestMapping("/user") public class UserController { private Map<String, User> users = new HashMap<String, User>(); public UserController() { users.put("sdy",new User("sdy","123","宋冬野","sdy@qq.com")); users.put("ldh",new User("ldh","123","刘德华","ldh@qq.com")); users.put("gfc",new User("gfc","123","郭富城","gfc@qq.com")); users.put("lm",new User("lm","123","黎明","lm@qq.com")); users.put("zxy",new User("zxy","123","张学友","zxy@qq.com")); } //列表页面 @RequestMapping(value = http://www.mamicode.com/"/users",method = RequestMethod.GET)>Rest风格
/user_show?id=120 /user/120 /user_delete?id=123 /user/123/delete /user_updateInput?id=123 /user/123/update /user_list /users /user/users REST的风格不等于使用了REST技术 GET,POST,PUT,DELETE topic/23/delete使用hibernate+spring+springmvc完整的实现一个用户管理系统
dao-->service-->controller
增加分页,增加sitemesh
/** * 局部异常处理,仅仅只能处理这个控制器中的异常 */ @ExceptionHandler(value=http://www.mamicode.com/{UserException.class})><!-- 将静态文件指定到某个特殊的文件夹中统一处理--> <mvc:resourceslocation="/resources/" mapping="/resources/**"/>Spring mvc 文文件上传
1,UserController.java
//在具体添加用户时,是post请求,就访问以下代码 @RequestMapping(value=http://www.mamicode.com/"/add",method=RequestMethod.POST)>2,Hello-servlet.xml
<!-- 设置multipartResolver才能完成文件上传--> <bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <propertyname="maxUploadSize" value=http://www.mamicode.com/"5000000">>3,jsp
<!-- 此时没有写action,直接提交会提交给/add--> <sf:form method="post"modelAttribute="user" enctype="multipart/form-data"> Username:<sf:inputpath="username"/><sf:errorspath="username"/><br/> Password:<sf:passwordpath="password"/><sf:errorspath="password"/><br/> Nickname:<sf:inputpath="nickname"/><br/> Email:<sf:inputpath="email"/><sf:errors path="email"/><br/> Attach:<inputtype="file" name="attachs"/><br/> <input type="file"name="attachs"/><br/> <inputtype="file" name="attachs"/><br/> <inputtype="submit" value=http://www.mamicode.com/"添加用户"/>>Sitemesh:
步骤:
1, 定义相应的模板文件(main.jsp)
2, 编写装饰器文件,说明哪些页面要引入模板
decorators.xml:
<?xmlversion="1.0" encoding="UTF-8"?> <decoratorsdefaultdir="/WEB-INF/decorators"> <!-- Any urls that are excluded willnever be decorated by Sitemesh --> <excludes> <pattern>/exclude.jsp</pattern> <pattern>/exclude/*</pattern> </excludes> <decorator name="main"page="main.jsp"> <pattern>/*</pattern> </decorator> </decorators>3, 在web.xml中开启sitemesh的过滤器
<filter> <filter-name>sitemesh</filter-name> <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class> </filter> <filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Spring mvc总结:
1,在xml中配置DispatcherServlet
2,在web-inf下配置:xxx-servlet.xml
3,异常处理分局部异常处理(handlerException方法),和全局异常处理(配置xxx-servlet:SimpleMappingExceptionResolver)
异常处理(handlerException方法示例: /** * 局部异常处理,仅仅只能处理这个控制器中的异常 */ @ExceptionHandler(value=http://www.mamicode.com/{UserException.class})>4,页面传值到Controller可以用方法的参数直接接收,或者用@PathVariable接收:
@RequestMapping(value = http://www.mamicode.com/"/{id}",method = RequestMethod.GET)>5,从Controller传值到页面可以从方法参数注入session,request,或者用Model,ModelMap
6,上传文件可以直接在方法参数中加:
@RequestParam("attach")MultipartFile //上传一个文件,file input的名称为attach @RequestParam("attachs")MultipartFile[] //上传一组文件,file input的名称都为attachsSpring mvc上传文件示例:
@RequestMapping(value=http://www.mamicode.com/"/add",method=RequestMethod.POST)>
7,spring mvc方法返回json对象示例:导入json包:jackson-all-1.9.4.jar
@RequestMapping(value=http://www.mamicode.com/"/{username}",method=RequestMethod.GET,params="json")>孔浩Spring mvc 笔记