zoukankan      html  css  js  c++  java
  • 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配置的视图位置就行了

  • 相关阅读:
    AJAX异步传输——以php文件传输为例
    js控制json生成菜单——自制菜单(一)
    vs2010中关于HTML控件与服务器控件分别和js函数混合使用的问题
    SQL数据库连接到服务器出错——无法连接到XXX
    PHP错误:Namespace declaration statement has to be the very first statement in the script
    【LeetCode】19. Remove Nth Node From End of List
    【LeetCode】14. Longest Common Prefix
    【LeetCode】38. Count and Say
    【LeetCode】242. Valid Anagram
    【LeetCode】387. First Unique Character in a String
  • 原文地址:https://www.cnblogs.com/sss-justdDoIt/p/9278971.html
Copyright © 2011-2022 走看看