实习笔记之Spring MVC(二)
来源:互联网 发布:阿里云国际注册 编辑:程序博客网 时间:2024/05/17 05:39
网上看到的一个很好的例子。发出来Mark一下。
Its always been fun for me to work with Ajax! Is not it ? I will make it easy for you
Let us see what is our example’s requirement and how Spring MVC 3 Ajax facility will fulfill it :
In our example, we will make a list of students with name and highest education level, to send the list to the placement office so that the students can get chance. We will make the “Add Student Form” available to the student online so that they can submit their name online and get registered. As a lot of students will use the system, so the performance of the system may very much low. To increase to performance of the web application we will use Ajax with Spring MVC 3 Framework and JQuery.
Following steps we have to go through to implement our example :
- First of all, we will create a domain class (User.java) that will hold the value of student information.
- After that we will create our controller class (UserListController.java) to handle HTTP request. Our controller will handle three types of requests. First, to show the “Add Student Form”, second to handle Ajax request came from “Add Student Form” and add the students to a list, third to show the student information as a list.
- Then, we will create jsp page
(AddUser.jsp) to show “Add Student Form” that will use JQuery to send Ajax request to the Spring MVC Controller. The jsp will also confirm to the user that Student has been added to the list. - Then, we will create a jsp (ShowUsers.jsp) that will list all users in the list.
User.java
User.java has two properties name and education to store the student information. Following is the code of User.java :
package com.raistudies.domain;
public class User {
}
Controllers has three method to handle three request urls. “showForm” method handle the request for showing the form to the user. Bellow code shows the UserListController.java :
package com.raistudies.controllers;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
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.ResponseBody;
import com.raistudies.domain.User;
@Controller
public class UserListController {
}
AddUser.jsp contain a simple form to collect information about the student and uses JQerey JavaScript framework to generate Ajax request to the server. Following is the code in AddUser.jsp :
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
</html>
ShowUsers.jsp
Following are the code in ShowUsers.jsp to print all student information from a ArrayList to jsp page :
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
</html>
- <c:forEach items=”${Users}” var=”user”>
: tag is used for iterate through the ArrayList. Property “items” is used to define the bean on which the List object has been stored, soitems=”${Users}” says that the users list is present in “Users” bean. “var” attribute says the name of the variable in which each user will be stored. - <c:out value=”${user.name}” />
: As, a single user will be stored in variable name “user” so to print the name property in User object we use ${user.name}.
app-config.xml
Our Spring MVC configuration file should be able to handle annotation driven controllers. The configuration are as follows :
<?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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
</beans>
- 实习笔记之Spring MVC(二)
- 实习笔记之Spring MVC
- 实习笔记之Spring MVC + ajax + highcharts
- Spring学习笔记之二: Spring MVC
- 实习笔记(二)
- Spring MVC 学习笔记(二)
- 【笔记】Spring MVC学习指南(二)MVC模式
- Java开发之Spring MVC(二)
- spring mvc+mybatis笔记(二)
- linux实习笔记(二)
- spring MVC+mybatis+spring security笔记<二>
- spring MVC学习笔记(二) springMVC.xml配置文件解析
- Spring MVC 学习笔记(二):@RequestMapping用法详解
- Spring MVC 学习笔记(二):@RequestMapping用法详解
- Spring MVC 学习笔记(二):@RequestMapping用法详解
- Spring MVC 学习笔记(二):@RequestMapping用法详解
- Spring学习笔记之Spring MVC体系学习(一)
- Spring MVC 学习笔记 二 spring mvc Schema-based configuration
- CToolTipCtrl实现鼠标移动显示不同提示
- 多线程理解1
- MySQL 数据(字段)类型
- HDU 2795 Billboard(线段树)
- linux内核分析笔记----定时器和时间管理
- 实习笔记之Spring MVC(二)
- Target is not a jdk root.System library was not found.
- UVa:714 Copying Books .
- .Net基础一、.net语法(c#)
- log4j配置
- eclypse关联android源码和v4源码
- 阅读/var/spool/mail/root中的邮件
- 系统设计说明书(架构、概要、详细)目录结构
- 初用cocostudio