构建环境
导入maven
配置web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <web-app> <display-name>JavaeeDemo</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/configs/spring/spring-bean.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
|
配置spring的xml
/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml
的配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <beans> <context:annotation-config/> <context:component-scan base-package="com.ahao.javaeeDemo"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsps/"/> <property name="suffix" value=".jsp"/> <property name="order" value="1000"/> </bean> </beans>
|
/WEB-INF/configs/spring/spring-bean.xml
的配置
1 2 3 4 5 6
| <beans> <context:annotation-config/> <context:component-scan base-package="com.ahao.javaeeDemo"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>
|
url映射
get、post方式匹配
使用@RequestParam
注入参数,封装成对象再传入model
,在jsp
页面中用EL
表达式获取即可。
1 2 3 4 5 6 7 8 9 10 11
| @Controller @RequestMapping("/hello") public class MyController { @RequestMapping(value = "/mvc", method = RequestMethod.GET) public String hello(@RequestParam("name") String name, // 匹配http://localhost:8080/hello/mvc?name=? Model model){ User user = new User(name); model.addAttribute(user); return "home"; } }
|
使用ServletAPI
直接注入HttpServletRequest
或者HttpSession
1 2 3 4 5 6 7 8 9 10 11
| @Controller @RequestMapping("/hello") public class MyController{ @RequestMapping(value="/mvc", method=RequestMathod.GET) public String hello(HttpServletRequest request){ String name = request.getParameter("name"); User user = new User(name); request.setAttribute("user",user); return "home"; } }
|
restful方式匹配
使用@PathVariable
注入参数
1 2 3 4 5 6 7 8 9 10
| @Controller @RequestMapping("/hello") public class MyController{ @RequestMapping(value="/mvc/{name}", method=RequestMethod.GET) public String hello(@PathVariable("name") String name, Map<String,Object> model){ User user = new User(name); model.put("user", user); return home; } }
|
文件上传
jsp提交页面
1 2 3 4
| <form method="post" action="<%=request.getContextPath()%>/hello/doUpload" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="submit" /> </form>
|
配置spring
1 2 3 4 5 6 7
| <beans> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="209715200"/> <property name="defaultEncoding" value="UTF-8"/> <property name="resolveLazily" value="true"/> </bean> </beans>
|
配置Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @Controller @RequestMapping("/hello") public class MyController{ @RequestMapping(value="/{name}", method=RequestMethod.GET) public String hello(@PathVariable("name") String name, Model model){ User user = new User(name); return "home"; } @RequestMapping(value="/doUpload",method=RequestMethod.POST) public String doUpload(@RequestParam("file") MultipartFile file) throws IOException{ if(!file.isEmpty()){ System.out.println("文件名:"+file.getOriginalFilename()); FileUtils.copyInputStreamToFile(file.getInputStream(), new File("D:\\", file.getOriginalFilename+".bak")); } return "redirect:"+file.getOriginalFilename(); } }
|
返回json格式数据
配置spring
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <beans> <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="ignoreAcceptHeader" value="true"/> <property name="mediaTypes"> <map> <entry key="json" value="application/json"/> <entry key="xml" value="application/xml"/> <entry key="htm" value="text/html"/> </map> </property> </bean> <bean class="org.springframework.web.servlet.view.ContentNegotiationViewResolver"> <property name="order" value="1"/> <property name="contentNegotiationManager" ref="contentNegotiationManager"/> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/> </list> </property> </bean> </beans>
|
配置Controller
1 2 3 4 5 6 7 8 9 10 11 12
| @Controller @RequestMapping("/hello") public class MyController{ @RequestMapping(value="/json1/{name}", method=RequestMethod.GET) public @ResponseBody User getUserJson1(@PathVariable("name") String name){ return new User(name); } @RequestMapping("value"/json2/{name}", method=ReuqestMethod.GET) public ResponseEntity<User> getUserJson2(@PathVariable String name){ return new ResponseEntity<User>(new User(name), HttpStatus.OK) } }
|
拦截器
- 实现
HandlerInterceptor
接口
- 实现
WebRequestInterceptor
接口 : 不能拦截请求