前面学了spirng的一些配置,以及web方面的知识,今天就在学习一下在spring比较经常使用的一些高级技术。。。html
首先来介绍下什么叫servlet吧(来着维基百科)java
Servlet(Server Applet),全称Java Servlet,未有中文译文。是用Java编写的服务器端程序。其主要功能在于交互式地浏览和修改数据,生成动态Web内容。狭义的Servlet是指Java语言实现的一个接口,广义的Servlet是指任何实现了这个Servlet接口的类,通常状况下,人们将Servlet理解为后者。web
Servlet运行于支持Java的应用服务器中。从实现上讲,Servlet能够响应任何类型的请求,但绝大多数状况下Servlet只用来扩展基于HTTP协议的Web服务器。spring
我刚开始没有接触到框架的时候,写的服务就是新建一个servlet,以后还知道了struct2 以后才知道spring框架。。json
spring in action 这本书中,推崇的配置是经常使用java来配置文件的。可是咱们好像在平时中经常使用的仍是xml配置。。先来看一下java注解来使用servlet的方法:服务器
public class MyServletConfig implements WebApplicationInitializer { public void onStartup(ServletContext servletContext) throws ServletException { Dynamic myServlet = servletContext.addServlet("myServlet", com.springmvc.servlet.TestServlet.class); myServlet.addMapping("/servlet"); } }
而后咱们在来看看xml的配置:session
<servlet> <servlet-name>servletTest</servlet-name> <servlet-class>com.springmvc.servlet.TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>servletTest</servlet-name> <url-pattern>/servlet</url-pattern> </servlet-mapping>
而后咱们建立一个调用接口HttpServlet的servlet:mvc
public class TestServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); PrintWriter pw = resp.getWriter(); pw.write("<h1> hello servlet!</h1>"); } }
部署到Tomcat上面输入 http://localhost:8080/servlet app
运行结果为:框架
这样一来简单的servlet如何在springmvc中使用咱们就学会了.
下面就让咱们来学一下filter的用法,首先是用java配置方式
public class MyFilterConfig implements WebApplicationInitializer { public void onStartup(ServletContext servletContext) throws ServletException { Dynamic myFilter = servletContext.addFilter("myFilter", com.springmvc.filter.TestFilter.class); myFilter.addMappingForUrlPatterns(null, false, "/back/*"); } }
下面是xml注解方式:
<filter> <filter-name>fileterTest</filter-name> <filter-class>com.springmvc.filter.TestFilter</filter-class> </filter> <filter-mapping> <filter-name>fileterTest</filter-name> <url-pattern>/back/*</url-pattern> </filter-mapping>
而后写个TestFilter类,这个是我之前写的一个简单判断是否登陆的一个filter,若是没有登陆的话,就跳转到登陆界面,只有登陆了以后才能继续访问某个文件夹,或者说某个项目下的东西:
public class TestFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) servletRequest; HttpServletResponse resp = (HttpServletResponse) servletResponse; HttpSession session = req.getSession(); //获取请求路径 String path = req.getRequestURI(); //获取session中做为判断的字段 String pwd = (String) session.getAttribute("passwd"); System.out.println(pwd); //判断请求的 路径中是否包含了 登陆页面的请求 //若是包含了,那么不过滤 继续执行操做 if (path.indexOf("/back/login.do") > -1 || path.indexOf("/back/isLogin.json") > -1) { filterChain.doFilter(req, resp); } else { //如不包含,那么就要判断 session中否有标志,若是没有标志,那么不让他看,让他去登陆,反之执行操做! if (pwd == null || "".equals(pwd)) { resp.sendRedirect("/back/login.do"); } else { filterChain.doFilter(req, resp); } } } public void destroy() { } }
由于没有写 http://localhost:8080/back/login.do 的请求方法,因此是404,运行结果以下:
上面就是在springmvc中用filter
下面继续来看multipart文件上传,这个用的也蛮多的,我通常图像啊,文件啊,上传都是用这个上传的。
通常来讲分为两种:
首先咱们来看看java配置:
@Bean public MultipartResolver multipartResolver() { CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); multipartResolver.setDefaultEncoding("utf-8"); multipartResolver.setMaxInMemorySize(40960); multipartResolver.setMaxUploadSize(10485760); return multipartResolver; }
xml的注解方式:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"/> <property name="maxInMemorySize" value="40960"/> <property name="maxUploadSize" value="10485760"/> </bean>
而后在页面中加上上传所需的组件:
<h1>Multipart上传测试</h1> <form id="form" enctype="multipart/form-data" action="/upload.do" method="post"> <input id="file" name="file" type="file"> <input type="submit" value="开始上传"> </form>
这里咱们要注意了 两个地方 enctype method 这两个参数 必定要有且后面的要为post要否则会报错
HTTP Status 500 - Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: The current request is not a multipart request
一开始我忘记配置 上传的配置了 报下面的错:
HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?
controller的编写也很简单,以下:
@RequestMapping(value = "/upload.do", method = RequestMethod.POST) public String upLoadFiles(MultipartFile file) { File tempFile = new File("d://multipart.jpg"); try { file.transferTo(tempFile); return "success"; } catch (IOException e) { e.printStackTrace(); return "error"; } }
结果就不上了,以上就是springmvc中 一些经常使用的其余技术,若是有错请指出,谢谢!