SpringMVC入门

作者: 间歇性丶神经病患者 | 来源:发表于2017-04-25 22:00 被阅读27次

环境的搭建

  • 使用工具:MyEclipse, tomcat7,SpringMVC3.2

  • 新建JAVA web Project,在WEB-INF/lib下导入相对应的jar包如下图:

Paste_Image.png
  • 配置WEB-INF/web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns: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/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>FirstWeb</display-name>
  <servlet>
    <description>JAX-RS Tools Generated - Do not modify</description>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <url-pattern>/jaxrs/*</url-pattern>
  </servlet-mapping>
  
  
  
  <!--springmvc 前端控制器 -->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器,适配器等等) -->
    <!-- 如果不配置这个东西,会默认加载/web-inf/servlet名称-servlet.xml -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
  
  
  <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>*.action</url-pattern>
  <!-- 
  
        第一种:*.action:访问以.action结尾由DispatcherServlet进行解析
        第二种:/:所有的访问都由DispatcherServlet进行解析 ,对于静态的文件的解析需要配置不让DispatcherServlet进行解析,
        使用这种方式的可以实现RESTful风格的url
        第三种:/*:这样配置不对,使用这种配置,最终都要转发到一个jsp页面,仍然会由DispatcherServlet进行解析,但是不能根据jsp页面找到headler,会报错。
   -->
  </servlet-mapping>
</web-app>

  • 其中我们指定了我们的前端控制器为:
<param-value>classpath:springmvc.xml</param-value>
  • 所以我们需要新建资源目录,与src平级:


    Paste_Image.png
  • 在里面新建立:springmvc.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:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
        
        
        <!-- 配置Handler -->
        <bean name="/queryItems.action"
        class="com.ly.springmvc.controller.ItemsController1"></bean>
        
        <!-- 處理器映射器
         将bean的name做为url来进行查找,需要在配置Handler时指定beanname
         (也就是url)
         -->
        
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
        
        <!-- 處理器適配器
            所有的適配器都要實現HandlerAdapter
         -->
        
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
        <!-- 視圖解析器 
            解析jsp视图,默认使用jstl标签,前提是得保证classpath下得有jstl的包
        -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</beans>
  • 基本搭建完成,那我们可以先写一个简单的Controller:
/**
 * 實現Controller的接口的处理器
 * @author Ly
 *
 */
public class ItemsController1 implements Controller {

    @Override
    public ModelAndView handleRequest(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {
        //调用Service查找数据库,查询商品列表
        List<Items> list=new ArrayList<Items>();
        list.add(new Items("Ly",11111,"这是凌宇"));
        list.add(new Items("Ly1",22222,"这是凌宇1"));
        list.add(new Items("Ly2",333333,"这是凌宇2"));
        
        // 返回ModelAndView
        ModelAndView modelAndView =new ModelAndView();
        // 相当于request的setAttribut,在jsp页面中通过itemsList获取数
        modelAndView.addObject("itemsList",list);
        // 指定视图
        modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
        return modelAndView;
    }

}
  • 由于我们指定了一个jsp目录,所以我们在目录下新建立一个jsp:
Paste_Image.png
  • 运行后得到结果:


    Paste_Image.png

使用注解模式

在上面的配置都是非注解模式,那么我们可以使用注解模式来进行开发:

<!-- 以上为非注解的东西,下面为注解的配置 -->
        <!-- 注解映射器 -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
        
        <!-- 注解适配器 -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>

相关文章

网友评论

  • sendtion:怎么从数据库取数据呢?使用myBatis

本文标题:SpringMVC入门

本文链接:https://www.haomeiwen.com/subject/wqjbzttx.html