Spring框架@SessionAttribute@ResponseBody等注解,要常看的Ajax回调函数【诗书画唱】
前言:建议自己有时间等情况时,多看看这篇专栏,因为Ajax回调函数的使用方法和部分的个人通透的理解等方面的内容,以后我可能常用。
概括:
边看教程视频边做的学习笔记等的记录(闲的时候可以复习看看)。
常用的注解。
个人理解:@controller注解的话就是把普通的java类转化为Controller。
在实现session信息共享的存储账号和密码等的登录等时用的@SessionAttribute注解。
使用@SessionAttribute注解实现模拟的简易登录功能。
在把User类u的内容“赋值”到@ModelAttribute后,@ModelAttribute把内容赋值存储到@sessionAttribute注解中,之后想使用其内容时就从中调用就可以,十分方便
Spring MVC框架中使用@SessionAttribute注解后,调用出其想拿出来的数据内容的方法
:因为@SessionAttribute注解存储是会放在Model中,所有调用其数据时要传Model
jsp中获取@SessionAttribute注解存储的内容还是要使用session.getAttribute方法
Spring MVC框架中使用HttpSession调用session存的数据是不可以的,可能在servlet中可以使用
这个方法写完后其@SessionAttribute注解才把数据放在session中
@SessionAttribute中放2个被赋予值的变量的方法
@ResponseBody注解的使用
@ResponseBody注解可以让内容被打印到网页界面上,进而可以被使用了Ajax技术的回调函数获取
面试操作题或问答题,项目实际操作等中常用的Ajax回调函数的将从网页获取的data字符串转换成js对象的2种方法:用eval方法或用JSON.parse方法
data字符串转换成js对象的第3种方法:在Ajax回调函数中使用dataType:”JSON“的话就是会自动把从网页上获取的data部分转换成js对象
如果{}为字符串开始结尾的,在这里是JSON字符串,如果是[]结尾的,这里就是数组,那么Ajax回调函数中要把已经使用dataType:”JSON“转化JSON字符串为js对象的部分从数值中通过for循环和下标遍历出来(个人的另一种理解:dataType:”JSON“使得出现JSON字符串后自动转为js对象,所以可以理解为数组中取出JSON字符串,之后JSON字符串才自动转化成js对象,我认为我个人的2种执行顺序的理解可能都对,但我更偏向认为这种理解更对。)
有时候eclipse会”脑抽筋“,多重启下等等就可以
当Ajax回调函数在网页中获取的内容是数组时,获取数组中所有数据的方法和获取其中的对象的某个数据的方法
@RequestBody注解的使用
使用@RequestBody注解前,可能比@ResponseBody注解的使用复杂点(但我认为其实都不是很复杂),必须要先导入我在图中框起来的3个包,可能官网上可以下载
让我们可以查看JSON字符串版本的对象的stringify方法
用map.put添加总数等的方法
5、SPRING框架_注解.ppt START
实例
实例部分可以对照边看教程视频边做的学习笔记等的记录来看

获取账号和密码
判断:
如果查到这条记录,登录成功,将输入的账号和密码放到session中,跳转页面
如果没有查到,就要提示登录失败
session.setAttribute("_user",user);


边看教程视频边做的学习笔记等的记录(闲的时候可以复习看看) START
常用的注解 START

个人理解:@controller注解的话就是把普通的java类转化为Controller。






在实现session信息共享的存储账号和密码等的登录等时用的@SessionAttribute注解

常用的注解 END
使用@SessionAttribute注解实现模拟的简易登录功能 START
在把User类u的内容“赋值”到@ModelAttribute后,@ModelAttribute把内容赋值存储到@sessionAttribute注解中,之后想使用其内容时就从中调用就可以,十分方便


Spring MVC框架中使用@SessionAttribute注解后,调用出其想拿出来的数据内容的方法
:因为@SessionAttribute注解存储是会放在Model中,所有调用其数据时要传Model

Spring MVC框架中使用@SessionAttribute注解后,调用出其想拿出来的数据内容的方法
:因为@SessionAttribute注解存储是会放在Model中,所有调用其数据时要传Model




使用@SessionAttribute注解实现模拟的简易登录功能 END
@ResponseBody注解的使用 START


面试操作题或问答题,项目实际操作等中常用的Ajax回调函数的将从网页获取的data字符串转换成js对象的2种方法:用eval方法或用JSON.parse方法

data字符串转换成js对象的第3种方法:在Ajax回调函数中使用dataType:”JSON“的话就是会自动把从网页上获取的data部分转换成js对象


如果{}为字符串开始结尾的,在这里是JSON字符串,如果是[]结尾的,这里就是数组,那么Ajax回调函数中要把已经使用dataType:”JSON“转化JSON字符串为js对象的部分从数值中通过for循环和下标遍历出来(个人的另一种理解:dataType:”JSON“使得出现JSON字符串后自动转为js对象,所以可以理解为数组中取出JSON字符串,之后JSON字符串才自动转化成js对象,我认为我个人的2种执行顺序的理解可能都对,但我更偏向认为这种理解更对。)

如果{}为字符串开始结尾的,在这里是JSON字符串,如果是[]结尾的,这里就是数组,那么Ajax回调函数中要把已经使用dataType:”JSON“转化JSON字符串为js对象的部分从数值中通过for循环和下标遍历出来(个人的另一种理解:dataType:”JSON“使得出现JSON字符串后自动转为js对象,所以可以理解为数组中取出JSON字符串,之后JSON字符串才自动转化成js对象,我认为我个人的2种执行顺序的理解可能都对,但我更偏向认为这种理解更对。)


@ResponseBody注解的使用 END
@RequestBody注解的使用 START














@RequestBody注解的使用 END
边看教程视频边做的学习笔记等的记录(闲的时候可以复习看看) END
5、SPRING框架_注解.ppt START

























5、SPRING框架_注解.ppt END
实例 START
实例部分可以对照边看教程视频边做的学习笔记等的记录来看

package com.SSHC.bean;
public class User {
private String act;
private String pwd;
private String Sex;
public String getAct() {
return act;
}
public void setAct(String act) {
this.act = act;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getSex() {
return Sex;
}
public void setSex(String Sex) {
this.Sex = Sex;
}
}

package com.SSHC.controller;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.SSHC.bean.User;
@Controller
//首先从request作用域中取出名为_user的对象
//然后再执行session.setAttribute("_user",u);
@SessionAttributes("_user")
public class PubController {
//request.setAttribute("_user",u);
@ModelAttribute("_user")
public User init(@RequestParam String act,
@RequestParam String pwd){
User u = new User();
u.setAct(act);
u.setPwd(pwd);
return u;
}
@RequestMapping("dl")
public String doLg(Model m){
User u = (User) m.asMap().get("_user");
return "manage";
}
}

package com.SSHC.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.SSHC.bean.User;
@Controller
public class ReqController {
@RequestMapping(value="reqCon",
method=RequestMethod.POST,consumes="application/json")
public @ResponseBody Map<String,Object> login(@RequestBody User u){
System.out.println(u.getAct());
System.out.println(u.getPwd());
System.out.println(u.getSex());
List<User>list = new ArrayList<User>();
User u1 = new User();
u1.setAct("Tom");
u1.setPwd("123");
list.add(u1);
User u2 = new User();
u2.setAct("KENF");
u2.setPwd("456");
list.add(u2);
Map<String,Object>map = new HashMap<String,Object>();
map.put("rows", list);
map.put("total", list.size());
return map;
}
}

package com.SSHC.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class RespController {
//http://localhost:8080/Spring7/resp
@RequestMapping("resp")
@ResponseBody
public String loadData(){
//response.getWrite().Writer("Hello world");
//return "{\"act\":\"admin\",\"pwd\":123}";
return "[\"one\",{\"name\":\"Tom\"},true,149]";
}
}


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base hreff="<%=basePath%>">
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" srcc="js/jquery-1.11.0.js"></script>
<script type="text/javascript">
$(function(){
$('#btn').click(function(){
$.ajax({
url: 'resp',
type: 'POST',
dataType: 'JSON',
success: function(data){
//将data字符串转换成js对象
//方法一:
//var o = eval('(' + data + ')');
//方法二:
//var o = JSON.parse(data);
//alert(o.act);
//alert(o.pwd);
//alert(data.act);
//for(var i = 0;i < data.length;i ++){
// alert(data[i]);
//}
alert(data[1].name);
}
});
});
$('#ts').click(function(){
var user = {};
user.act = 'Kite';
user.pwd = '456';
user.Sex = '男';
$.ajax({
url: 'reqCon',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(user),
success: function(data){
alert(data.total);
var rows = data.rows;
for(var i = 0;i < rows.length;i ++){
var u = rows[i];
alert(u.act);
}
}
});
});
})
</script>
</head>
<body>
<input id="btn" type="button" value="获取数据"/>
<input id="ts" type="button" value="传递参数到后台" />
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base hreff="<%=basePath%>">
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<style type="text/css">
*{
font-size: 50px;
}
</style>
</head>
<body>
<form action="dl" method="post">
<input type="text" name="act" placeholder="请输入账号"/>
<br>
<input type="password" name="pwd" placeholder="请输入账号"/>
<br>
<input type="submit" value="登录" />
</form>
</body>
</html>

<%@page import="com.SSHC.bean.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
User u = (User)session.getAttribute("_user");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base hreff="<%=basePath%>">
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<h1>登录成功<%=u.getAct() %></h1>
</body>
</html>

<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- 扫描指定的包和它的子包中的所有java类 -->
<!-- 只要是创建在com.SSHC.controller包下面的java类,只要给它添加了@Controller注解
那么这个java类就变成了springmvc中的一个controller -->
<context:component-scan base-package="com.SSHC.controller"/>
<!-- 默认注册RequestMappingHandlerMapping和RequestMappingHandlerAdapter类 -->
<mvc:annotation-driven />
<!-- jsp引用外部js,css等静态资源的解决方法(和上面的标签必须同时出现,否则无法访问url) -->
<mvc:default-servlet-handler />
<!-- 配置视图名称解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<!-- 将所有的jsp文件存放在/WEB-INF/my/目录下 -->
<property name="prefix" value="/WEB-INF/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
<!-- 优先级设定 -->
<property name="order" value="10"></property>
</bean>
<!-- http://localhost:8080/Spring7/aaa -->
<mvc:view-controller path="/aaa" view-name="lg"/>
<mvc:view-controller path="/bbb" view-name="demo"/>
</beans>

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>J190802</display-name>
<!-- controller中文乱码处理,注意一点:要配置在所有过滤器的前面 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- springmvc框架配置 -->
<!-- DispatcherServlet是前置控制器,配置在web.xml文件中的。
拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,
依据相应的规则分发到目标Controller来处理,
是配置spring MVC的第一步。 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 覆盖默认配置文件{servlet-name}-servlet.xml -->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

访问路径和对应配置文件代码等个人写的说明.text
http://localhost:8080/Spring7/aaa : <mvc:view-controller path="/aaa" view-name="lg"/>
http://localhost:8080/Spring7/bbb :<mvc:view-controller path="/bbb" view-name="demo"/>
【在这里,lg就是lg.jsp,demo就是demo.jsp】
http://localhost:8080/Spring7/resp :
@Controller
public class RespController {
@RequestMapping("resp")
@ResponseBody
public String loadData(){
//response.getWrite().Writer("Hello world");
//return "{\"act\":\"admin\",\"pwd\":123}";
return "[\"one\",{\"name\":\"Tom\"},true,149]";
}














