发表于: 2018-03-20 16:36:54

1 401


今天完成的事情:

1.搭建springMVC框架。

(SpringMVC学习笔记(2)——商品列表查询入门程序  https://crowhawk.github.io/2017/04/10/SpringMVC_2/)

1.1文件结构和webxml文件。监听器<listener>在这个例子中不是必须,而且添加之后运行时报错找不到“/WEB-INF/applicationContext.xml”,所以我在对应位置建了一个空的。还有有看到jsp文件夹正确时应该建在webapp下的,这里可能(运行了几次只报错一次)会报错“No mapping found for HTTP request with URI”需要在springmvc-servlet.xml文件中添加一句<mvc:resources mapping="/jsp/**" location="/WEB-INF/jsp/"/>

1.2springmvc-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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

   <!--配置Handler即Controller,指定url给需要访问的Controller-->
   <bean id="/queryItems.action" class="com.crow.ssm.controller.ItemsController"/>
   <!--处理器映射器-->
   <!-- 处理器映射器 将bean的name作为url进行查找,需要在配置Handler时指
   定beanname(就是url) 所有的映射器都实现了HandlerMapping接口 -->
   <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
   <!-- 处理器适配器 -->
   <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>

<!--视图解析器 视图解析器 -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <!-- 配置jsp路径的前缀 -->
       <property name="prefix" value="/WEB-INF/jsp/"/>
       <!-- 配置jsp路径的后缀 -->
       <property name="suffix" value=".jsp"/>
   </bean>
   <!--2018年3月20日15点41分No mapping found for HTTP request with URI报错加这句,就是文件的位置
   <mvc:resources mapping="/jsp/**" location="/WEB-INF/jsp/"/>-->
</beans>

1.3运行结果(其他文件看教程链接就好)。重点时url和教程中的不一样。

2.servlet生命周期

(Servlet基础  https://www.cnblogs.com/crowsong/p/6374106.html)

a.初始化阶段,调用init()方法
b.响应客户请求阶段,调用servlet()方法。由servlet()方法根据提交方式选择执行doGet()或者doPost()方法。
c.终止阶段,调用destroy()方法


明天的计划:

用注解的方式完成SSM框架

遇到的问题:

url的具体过程不清楚,所以在tomcat运行时访问一直出错。想想应该是教程中Controller的bean的id就是“/springmvc/querytems.action"。

收获:

定义servlet元素时如果没有load-on-startup,在输入url时等的时间比较长。

servlet的生命周期。


返回列表 返回列表
评论

    分享到