发表于: 2020-01-09 18:15:15
1 1277
今天完成的事情:
1.完成步骤三
首先:引入依赖,先指定json-taglib所在仓库,进行下载
<repositories>
<repository>
<id>java.net</id>
<url>http://maven.jahia.org/maven2</url>
</repository>
</repositories>
<dependency>
<groupId>atg.taglib.json</groupId>
<artifactId>json-taglib</artifactId>
<version>0.4.1</version>
</dependency>
运行结果:
UserController
package com.wp.json.controller;
import com.github.pagehelper.PageHelper;
import com.wp.json.beans.User;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping(value ="/insertview",method = RequestMethod.GET)
public ModelAndView addUserView(ModelAndView mv) {
mv.setViewName("insert");
return mv;
}
@RequestMapping(value = "/json",method= RequestMethod.POST)
public ModelAndView insertUser(@ModelAttribute User user, ModelAndView mv) throws Exception {
user.setName(user.getName());
user.setQq(user.getQq());
user.setBro(user.getBro());
mv.addObject("user",user);
mv.setViewName("json");
return mv;
}
@RequestMapping(value = "/json1",method= RequestMethod.GET)
public ModelAndView insertUser1(User user, ModelAndView mv) throws Exception {
user.setName("enen");
user.setQq(1233333);
user.setBro("广告歌");
mv.addObject("user",user);
mv.setViewName("json");
return mv;
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>查询用户</title>
</head>
<body>
<a href="/user/insertview">添加用户</a>
</body>
</html>
insert.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加学生信息</title>
</head>
<body>
<h2>添加学生信息</h2>
<form id="insertForm" action="/user/json" method="post">
<input type="hidden" name="_method" value="POST">
<table align="center" bgcolor="lightgrey" border="1" cellpadding="0">
<tr>
<td>姓名:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>QQ:</td>
<td><input type="text" name="qq"/></td>
</tr>
<tr>
<td>师兄:</td>
<td><input type="text" name="bro"/></td>
</tr>
<input type="submit" value="添加"/>
</table>
</form>
</body>
</html>
json.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="json" uri="http://www.atg.com/taglibs/json" %>
<html>
<head>
<title>Json</title>
</head>
<body>
<json:object>
<json:property name="name" value="${user.name}"/>
<json:property name="qq" value="${user.qq}"/>
<json:property name="bro" value="${user.bro}"/>
</json:object>
</body>
</html>
明天计划的事情:
继续任务
遇到的问题:
1.刚开始运行时报400错误,可能原因是我用表格存放了其他多余的数据,因为其他数据的干扰导致数据类型的报错。
2.Jetty就是不好使
收获:
了解了Json的作用及简单的操作方式。
评论