zoukankan      html  css  js  c++  java
  • Spring MVC学习初篇

    Spring mvc 使用配置:

     <!-- 使用MVC -->
      <servlet>
          <servlet-name>defaultDispatcher</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <!-- Spring XML 文件 -->
              <param-value>classpath:school-context.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
          <servlet-name>defaultDispatcher</servlet-name>
          <url-pattern>/*</url-pattern>
      </servlet-mapping>
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"  
     xmlns:context="http://www.springframework.org/schema/context"  
     xmlns:p="http://www.springframework.org/schema/p"  
     xmlns:mvc="http://www.springframework.org/schema/mvc"  
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     xsi:schemaLocation="http://www.springframework.org/schema/beans  
          http://www.springframework.org/schema/beans/spring-beans-3.0.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-3.0.xsd">
    <bean name="/test1/helloworld" class="com.yinuo.web.controller.HelloWorldController" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>

      访问静态资源(js、css、img等)

    <!-- 访问静态资源   -->
    <mvc:resources location="/js/" mapping="/js/**"/>
    <mvc:resources location="/img/" mapping="/img/**"/>
    <mvc:resources location="/css/" mapping="/css/**"/>

    Spring  mvc 注解:

    <!-- 注解扫描包 -->
        <context:component-scan base-package="cn.com.yinuo.school"></context:component-scan>
        
        <!-- 开启注解 -->
        <mvc:annotation-driven/>
        <!-- 上面开启注解的方式是下面开启注解方式的优化,下面的方式在3之后就过时了 -->
        <!--  
            <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
            <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
         -->
    @Controller
    @RequestMapping("/book")
    public class UserController { @RequestMapping(value="/addBook",method=RequestMethod.POST) public ModelAndView addBook(){ String result ="this is addBook------"; return new ModelAndView("/bookList","result",result); } @RequestMapping(value="/delBook",method=RequestMethod.GET) public ModelAndView delUser(){ String result ="this is delBook------"; return new ModelAndView("/bookList","result",result); }
       //去掉method=RequestMethod.GET /.POST表示两者皆可 @RequestMapping(
    "/updateBook") public String updateBook(){ return "/bookList"; } }

    Spring 参数传递:

      单个域传递:

      Controller方法的参数名字与表单域的名字一致,即可直接获得表单的数据

      对象封装传递:

      封装到一个bean里面。bean的属性名与表单域的name属性名称一致,并且bean提供getter和setter方法,在controller即可直接bean.getXxx()获得。

      json传递 

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
       
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>新增书籍</title>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#add").click(function(){
                var bookName = $("#bookName").attr("value");
                var price =$("#price").attr("value");
                var book = {bookName:bookName,price:price};
                $.ajax({
                    url:"/book/addBook",
                    type:"post",
                    data:book,
                    success:function(bk){
                        console.info("bookName--->" + bk.bookName + "price--->" + bk.price );
                    }
                });
            });
        });
    </script>
    </head>
    <body>
        <h>新增书籍</h>
        书名<input type="text" id="bookName" name="bookName">
        价格<input type="text" id="price" name="price">
        <input type="button" id="add" value="新增">
    </body>
    </html>
    @Controller
    @RequestMapping("/book")
    public class BookController {
        @RequestMapping("/addBook")
        public void addUserJson(Book book,HttpServletRequest request,HttpServletResponse response){
            String result = "{"bookName":" "+ book.getBookName() +" ","price":" "+ book.getPrice()+" "}";
            PrintWriter out = null;
            response.setContentType("application/json");
            try {
                out = response.getWriter();
                out.write(result);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    PEB结构学习
    2016元旦总结
    pwnable.kr的passcode
    PE文件格式(加密与解密3)(一)
    IDA的脚本IDC的一个简单使用
    Python3的tkinter写一个简单的小程序
    2016/12/3-问鼎杯线上赛1-1-Misc
    2016/12/3-问鼎杯线上赛6-2逆向分析
    redis取经之路
    springboot踩坑出坑记
  • 原文地址:https://www.cnblogs.com/loveyixiang/p/4388535.html
Copyright © 2011-2022 走看看