• java-springmvc配置


    springmvc的jar包保存在百度云盘jar目录下,本例中还有三个jackson的包,都在百度云盘jar目录下。

    1.xml配置

      web-app节点下增加

      <!-- 配置SpringMVC前端控制器 -->
      <servlet>
        <servlet-name>springmvc-test</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <!-- 指定SpringMVC配置文件 -->
        <!-- SpringMVC的配置文件的默认路径是/WEB-INF/${servlet-name}-servlet.xml -->
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:config/springmvc.xml</param-value>
        </init-param>
      </servlet>
      <servlet-mapping>
        <servlet-name>springmvc-test</servlet-name>
        <!-- 设置所有以action结尾的请求进入SpringMVC -->
        <url-pattern>*.action</url-pattern>
      </servlet-mapping>

    2.src下增加config文件夹(xml配置路径放在config,这个自己随意,xml对应位置就行),config增加springmvc.xml增加内容

      <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
      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-4.0.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

        <!-- 注解驱动 -->
        <mvc:annotation-driven />

        <!-- 自动扫描controller -->
        <context:component-scan base-package="springmvc.controller"/>

        <!-- 配置试图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <!-- 配置逻辑视图的前缀 -->
          <property name="prefix" value="/WEB-INF/jsp/"/>
          <!-- 配置逻辑视图的后缀 -->
          <property name="suffix" value=".jsp"/>
        </bean>
      </beans>

    3.controller大概的几种写法

    @Controller
    @RequestMapping("itme")
    public class ItemController {

      /**
      * http://localhost:8080/springmvc1/item/itemList.action
      * @return
      */
      @RequestMapping("/itemList.action")
      public ModelAndView queryItemList() {
        List<Item> list = new ArrayList<>();
        list.add(new Item(1, "张三", 2399, new Date(), "人物1"));
        list.add(new Item(2, "李四", 2399, new Date(), "人物2"));
        list.add(new Item(3, "王五", 2399, new Date(), "人物3"));

        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("itemList", list);
        modelAndView.setViewName("itemList");

        return modelAndView;
      }

      /**
      * http://localhost:8080/springmvc1/item/querylist.action
      * @return
      */
      @RequestMapping("/querylist.action")
      public String querylist() {
        return "itemList";
      }

      /**
      * 返回json,注意引用jackson包,有三个
      * http://localhost:8080/springmvc1/item/query.action
      * @return
      */
      @ResponseBody
      @RequestMapping("/query.action")
      public List<Item> query() {
        List<Item> list = new ArrayList<>();
        list.add(new Item(1, "张三", 2399, new Date(), "人物1"));
        list.add(new Item(2, "李四", 2399, new Date(), "人物2"));
        list.add(new Item(3, "王五", 2399, new Date(), "人物3"));
        return list;
      }
    }

    4.html就不贴了,看清楚springmvc.xml配置的视图位置就行了

  • 相关阅读:
    APP与智能手表是如何通信的【本文摘抄自深圳尚锐科技】
    flash添加超链接的办法
    NPOI使用手册
    如何在 SQL Server 实例之间传输登录和密码
    CentOS 7 上部署Mono 4 和Jexus 5.6
    django models 中choices之用法举例
    django中的objects.get和objects.filter方法的区别
    Django中的Model.objects.create() 和 Model() 的区别?
    python爬虫练手项目快递单号查询
    根据现有的数据库自动生成Django model
  • 原文地址:https://www.cnblogs.com/sss-justdDoIt/p/9278971.html
Copyright © 2020-2023  润新知