zoukankan      html  css  js  c++  java
  • 处理器方法的返回值

    处理器方法的返回值:

    • ModelAndView:既要传递数据又要跳转资源
    • String     跳转字符资源,不能传递数据,可以用model、map、modelmap来承载数据

    • void
     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.7.2.min.js"></script>
     8 <script type="text/javascript">
     9     $(function(){
    10         $("#myButton").click(function(){
    11             $.ajax({
    12                 url:"${pageContext.request.contextPath}/springmvc/hello",
    13                 type: "POST",
    14                 success: function(data){
    15                     var data1 = eval("("+data+")");
    16                     alert(data1.flavor);
    17                 }
    18             })
    19         })
    20         
    21     })
    22 </script>
    23 <title>Insert title here</title>
    24 </head>
    25 <body>
    26     <button id="myButton">点击我呀!</button>
    27 </body>
    28 </html>
     1 package com.bjsxt.handlers;
     2 
     3 import java.io.IOException;
     4 
     5 import javax.servlet.http.HttpServletResponse;
     6 
     7 import org.springframework.context.annotation.Scope;
     8 import org.springframework.stereotype.Controller;
     9 import org.springframework.web.bind.annotation.RequestMapping;
    10 
    11 //后台控制器
    12 @Controller
    13 @Scope("prototype")
    14 @RequestMapping("/springmvc")
    15 public class MyController{
    16 //接收json字符串封装成对象
    17 @RequestMapping(value="/hello",produces="text/html;charset=utf-8")
    18 public void hello1(HttpServletResponse response) throws IOException{
    19     String json="{"name":"weilong","flavor":"hot"}";
    20     response.getWriter().print(json);
    21 }
    22     
    23 }
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:context="http://www.springframework.org/schema/context" 
     5     xmlns:mvc="http://www.springframework.org/schema/mvc"
     6     xmlns:tx="http://www.springframework.org/schema/tx"
     7     xmlns:aop="http://www.springframework.org/schema/aop"
     8     xsi:schemaLocation="
     9         http://www.springframework.org/schema/beans 
    10         http://www.springframework.org/schema/beans/spring-beans.xsd
    11         http://www.springframework.org/schema/tx 
    12         http://www.springframework.org/schema/tx/spring-tx.xsd
    13         http://www.springframework.org/schema/aop 
    14         http://www.springframework.org/schema/aop/spring-aop.xsd
    15         http://www.springframework.org/schema/mvc
    16         http://www.springframework.org/schema/mvc/spring-mvc.xsd
    17         http://www.springframework.org/schema/context 
    18         http://www.springframework.org/schema/context/spring-context.xsd">
    19     <!-- 注册组件扫描器 -->
    20     <context:component-scan base-package="com.bjsxt.handlers"></context:component-scan>
    21     <!-- 注册注解驱动 -->
    22     <mvc:annotation-driven/>
    23     <!-- 注册视图解析器 -->
    24     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    25         <property name="prefix" value="/jsp/"></property>
    26         <property name="suffix" value=".jsp"></property>
    27     </bean>
    28         
    29     <!-- 静态资源无法访问第二种解决方案 -->
    30     <!-- <mvc:default-servlet-handler/> -->
    31     <!-- 静态资源无法访问第三种解决方案 -->
    32     <mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
    33     <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
    34 </beans>
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
     3   <display-name>springmvc-01-primary</display-name>
     4   <welcome-file-list>
     5     <welcome-file>index.jsp</welcome-file>
     6   </welcome-file-list>
     7   <!-- 静态资源无法访问第一种解决方案 -->
     8   <!-- 
     9   <servlet-mapping>
    10       <servlet-name>default</servlet-name>
    11       <url-pattern>*.png</url-pattern>
    12   </servlet-mapping>
    13   <servlet-mapping>
    14       <servlet-name>default</servlet-name>
    15       <url-pattern>*.js</url-pattern>
    16   </servlet-mapping>
    17   <servlet-mapping>
    18       <servlet-name>default</servlet-name>
    19       <url-pattern>*.css</url-pattern>
    20   </servlet-mapping>
    21    -->
    22   <!-- 注册springmvc前端控制器(中央调度器) -->
    23   <servlet>
    24       <servlet-name>springmvc</servlet-name>
    25       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    26       <!-- 指定springmvc配置文件的路径以及名称 -->
    27       <init-param>
    28           <param-name>contextConfigLocation</param-name>
    29           <param-value>classpath:springmvc.xml</param-value>
    30       </init-param>
    31   </servlet>
    32   <servlet-mapping>
    33       <servlet-name>springmvc</servlet-name>
    34       <url-pattern>/</url-pattern>
    35   </servlet-mapping>
    36 </web-app>
    • Object(涉及注解@ResponseBody,注册mvc注解驱动,导入jackson2.5包)
     1 package com.bjsxt.handlers;
     2 
     3 import org.springframework.context.annotation.Scope;
     4 import org.springframework.stereotype.Controller;
     5 import org.springframework.web.bind.annotation.RequestMapping;
     6 import org.springframework.web.bind.annotation.ResponseBody;
     7 
     8 //后台控制器
     9 @Controller
    10 @Scope("prototype")
    11 @RequestMapping("/springmvc")
    12 public class MyController{
    13 //接收json字符串封装成对象
    14 @RequestMapping(value="/hello",produces="text/html;charset=utf-8")
    15 @ResponseBody
    16 public Object hello1() {
    17     return "aaa";
    18 }
    19     
    20 }
     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.7.2.min.js"></script>
     8 <script type="text/javascript">
     9     $(function(){
    10         $("#myButton").click(function(){
    11             $.ajax({
    12                 url:"${pageContext.request.contextPath}/springmvc/hello",
    13                 type: "POST",
    14                 success: function(data){
    15                     alert(data);
    16                 }
    17             })
    18         })
    19         
    20     })
    21 </script>
    22 <title>Insert title here</title>
    23 </head>
    24 <body>
    25     <button id="myButton">点击我呀!</button>
    26 </body>
    27 </html>
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:context="http://www.springframework.org/schema/context" 
     5     xmlns:mvc="http://www.springframework.org/schema/mvc"
     6     xmlns:tx="http://www.springframework.org/schema/tx"
     7     xmlns:aop="http://www.springframework.org/schema/aop"
     8     xsi:schemaLocation="
     9         http://www.springframework.org/schema/beans 
    10         http://www.springframework.org/schema/beans/spring-beans.xsd
    11         http://www.springframework.org/schema/tx 
    12         http://www.springframework.org/schema/tx/spring-tx.xsd
    13         http://www.springframework.org/schema/aop 
    14         http://www.springframework.org/schema/aop/spring-aop.xsd
    15         http://www.springframework.org/schema/mvc
    16         http://www.springframework.org/schema/mvc/spring-mvc.xsd
    17         http://www.springframework.org/schema/context 
    18         http://www.springframework.org/schema/context/spring-context.xsd">
    19     <!-- 注册组件扫描器 -->
    20     <context:component-scan base-package="com.bjsxt.handlers"></context:component-scan>
    21     <!-- 注册注解驱动 -->
    22     <mvc:annotation-driven/>
    23     <!-- 注册视图解析器 -->
    24     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    25         <property name="prefix" value="/jsp/"></property>
    26         <property name="suffix" value=".jsp"></property>
    27     </bean>
    28         
    29     <!-- 静态资源无法访问第二种解决方案 -->
    30     <!-- <mvc:default-servlet-handler/> -->
    31     <!-- 静态资源无法访问第三种解决方案 -->
    32     <mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
    33     <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
    34 </beans>
  • 相关阅读:
    element-ui upload 上传图片之前压缩
    字符串截取substring放法传参不同返回不同
    vue中对于图片是否正常加载的思考
    前端图片合成并下载
    vue中图相对路径引用本地图片
    js计算精度
    vue-cli定义全局过滤器
    js加减乘除运算丢失精度 前端计算金额带小数点精度丢失问题
    鼠标样式大全
    js两小时倒计时,剩余时间倒计时,倒计时
  • 原文地址:https://www.cnblogs.com/wq-9/p/10174235.html
Copyright © 2011-2022 走看看