标签:静态 表示 can tag pack 500错误 请求 大于 val
2.springmvc的流程
三、写一个简单的springMVC的demo
1.引入springMVC相关的jar包 spring-beans spring-context spring-core spring-web spring-webmvc spring-expression commons-logging 缺少jar包提示: Caused by: java.lang.NoClassDefFoundError:Lorg/apache/logging/Log; ==> commons-logging.jar 2.生成web.xml并配置DispatcherServlet 在没有插件的前提下可以ctrl+shift+t检索获取完整类名 <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- init-param:手动配置读取的文件,DispatcherServlet(前端控制器)默认读取WEB-INF/servletName-servlet.xml 例如上面的servlet对应springMVC配置文件位置和命名必须为WEB-INF/springMVC-servlet.xml 没有手动配置,而配置名页不规范会报status500错误 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC.xml</param-value> </init-param> <!-- load-on-startup:当值为0或者大于0时,表示容器在应用启动时就加载这个servlet; 当是一个负数时或者没有指定时,则指示容器在该servlet被选择时才加载; 正数的值越小,启动该servlet的优先级越高; 将load-on-startup设置重复也不会出现异常,服务器会自己决定初始化顺序 advantages:如果初始化过程失败,则容器会提示启动失败,能够提前知道相关错误; 将初始化servlet的工作转移到容器启动过程,只要启动成功,就可立即响应web请求 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>*.do</url-pattern> <!--表示处理所有.do后缀的页面请求--> <!-- 拦截所有的请求(要注意防止静态资源的放行,如jpg,css,js,jquery,需要在springMVC配置文件中添加<mvc:default-servlet-handler/>) <url-pattern>/</url-pattern> --> </servlet-mapping> 3.springMVC配置文件(WEB-INF文件夹下) step1.配置HandlerMapping类(可以不写,已经自动配置) //将来用AnnotationHandlerMapping注解形式居多 <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> </bean> step2.写controller类继承AbstractController并重写方法 public class FirstController extends AbstractController { //返回类型可以是ModeAndView, String,Java对象(json) @Override //HttpServletRequest,HttpServletResponse如果报错可能没有配置Tomcat==>buildpath... protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { ModelAndView mv=new ModelAndView(); mv.setViewName("login"); //跳转界面 WEB-INF/view/logoin.jsp mv.addObject("user","user"); //保存数据 return mv; } } step3.springMVC-servlet.xml注册controller类 <bean id="/my.do" class="com.zhiyou100.yj.controller.FirstController"> <!--处理my.do请求--> </bean> step4.配置视图解析器 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"></property> <property name="suffix" value=".jsp"></property> </bean> 4.操作过程 step1.客户发出请求 http://localhost:8080/SpringMVC/my.do step2.到达web.xml中DispatcherServlet,查看是否符合拦截url的要求 step3.DiapatcherServlet查询springMVC的配置文件,找打HandlerMapping step4.根据bean的名称查找响应的Controller //这里使用的是BeanNameUrlHandlerMapping step5.进入匹配的Controller类,执行重写的handleRequestInternal方法 //将来会使用注解的形式 step6.根据返回的modelAndView,使用springMVC配置文件中的视图解析器 //把viewName与prefix及suffix做拼接,呈现给用户拼接后的页面 5.springMVC总览 <?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: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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> </bean> <bean id="/my.do" class="com.zhiyou100.yj.controller.FirstController"> </bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
四、handlerMapping的种类
1.根据根据bean的名称==>BeanNameUrlHandlerMapping(默认) <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> </bean> <bean id="/my.do" class="com.zhiyou100.yj.controller.FirstController"> </bean> 2.根据controller的类查找对应的controller类==>controllerClassNameHandlerMapping <!-- 地址栏:类名除了Controller其他一律小写.do,如:localhost:8080/SpringMVC/secondController.do --> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"> </bean> <bean class="com.zhiyou100.yj.controller.SecondController"> </bean> 3.id查询url地址与id对应的关系==>SimplerUrlHandlerMapping <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/a.do">dowhat</prop> <prop key="/b.do">dowhat</prop> </props> </property> </bean> <bean id="dowhat1" class="com.zhiyou100.yj.controller.SecondController"> </bean> <bean id="dowhat2" class="com.zhiyou100.yj.controller.SecondController"> </bean>
五、使用注解替代HandlerMapping的配置信息
1.添加spring-aop的jar包 2.springMVC配置文件 <!-- 1.包扫描:扫描注解所在的包 --> <context:component-scan base-package="com.zhiyou100.yj.controller.annotation"/> <!-- 2.开启注解驱动 --> <mvc:annotation-driven/> 3.创建Controller类(此时类不再需要继承AbstractController) @Controller //注册Controller类 @RequestMapping("user") //为类加注解,访问地址要加类名 public class UserController { @RequestMapping("getInfo.do") //访问地址 public String getInfo(User user) { //参数过多,可以封装到实体类,请求参数名一定要和实体类中属性名相同(不匹配项为null) System.out.println("获取信息"); return("login"); } @RequestMapping("setInfo") public ModelAndView setInfo(User user) { ModelAndView mv=new ModelAndView(); mv.setViewName("login"); mv.addObject("user","user"); return mv; } }
六、通过spring接收提交的参数
1.超链接传参: <a href="user/getInfo.do?name=zs"></a> 2.Controller接收参数 @Controller @RequestMapping("user") public class UserController { @RequestMapping("getInfo.do") public String getInfo(String name) { //参数一定要和请求的参数名相同 System.out.println("name:"+name); return("login"); } 3.防止接收的参数乱码 方法一:手栋设置添加编码过滤器 public class EncodingFilter implements Filter{ @Override public void destroy() { } @Override public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException,ServletException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); chain.doFilter(request, response); } @Override public void init(FilterConfig filterConfig) throws ServletException { } } web.xml下配置 <filter> <filter-name>encodingFilter</filter-name> <filter-class>com.zhiyou100.yj.filter.EncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 方法一:spring封装的过滤器==>spring-web==>org.springframework.web.filter==>CharacterEncodingFilter.class 配置文件经过编译后依然可以更改,而自己写的类都会变成字节码,用spring框架的内置过滤器可以对编码自由更改 <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
标签:静态 表示 can tag pack 500错误 请求 大于 val
原文地址:https://www.cnblogs.com/BoxMonster/p/11455845.html