从零到一 新手教程 - JAVA Spring MVC

简单明了的Spring MVC 基础教学. 

该简单工程的功能流程 : 1. 打开一个 jsp 页面输入必要的信息     2.  将输入的信息存储到 obejct 中    3. 将存储在 object 中的信息传输到另一个 jsp页面并显示.  


1. 建立 dynamic Web project, 并创建以下文件, 文件路径如图显示


从零到一 新手教程 - JAVA Spring MVC_第1张图片 


2. 下载 jar 包 并添加

从这个链接下载. 点我.  (我使用的是4.0.4版本的)  复制粘贴到lib文件夹中, 然后选中所有你刚粘贴到lib中的jar, 右键 build path -> add ....


3.  web.xml 代码如下:



  SpringMVCTutorial
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
  
  	spring-dispatcher
  	
  		org.springframework.web.servlet.DispatcherServlet
  	
  
  
  
  	spring-dispatcher
  	/
  
  

解释: 个人理解, 因为即使是spring mvc 但是它还是java web project (servlet等), 所以所有的请求都首先会来到web.xml这个配置文件, 然后根据这个文件来进行接下来的操作, 从以上代码可以看出来, 我们配置了一个servlet 名称为sping-dispatcher.  所以无论什么请求只要来到web.xml,那么都会被映射到spring-dispatcher-servlet.xml中.  


sping-dispatcher.xml  代码如下:




	
	
	
	
		/WEB-INF/
		
	
		.jsp
	
	

	

解释: 刚才说到所有请求会从web.xml 来到 spring-dispatcher-servlet.xml中, 在这个里面 重要的是 , 和.

 这行代码, 告诉spring mvc 我们的controller 类到包含在这个包里面了, 想要对从web.xml里面来的那个请求进行处理,就从这个包里面找好了. 

... 这个里面包含的所有东西, 就有一个目的就是 对 从 controller 中返回的请求进行寻找相对应的jsp/html等文件来显示.



Student.java 代码如下:

package com.tutorial.studentadmissioncontroller;

import java.util.ArrayList;
import java.util.Date;

public class Student {
	private String studentName;
	private String studentHobby; 
	private Long studentMobile;
	private Date studentDOB;
	private ArrayList studentSkills;
	private Address address;
	
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public Date getStudentDOB() {
		return studentDOB;
	}
	public void setStudentDOB(Date studentDOB) {
		this.studentDOB = studentDOB;
	}
	public ArrayList getStudentSkills() {
		return studentSkills;
	}
	public void setStudentSkills(ArrayList studentSkills) {
		this.studentSkills = studentSkills;
	}
	public Long getStudentMobile() {
		return studentMobile;
	}
	public void setStudentMobile(Long studentMobile) {
		this.studentMobile = studentMobile;
	}
	public String getStudentName() {
		return studentName;
	}
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}
	public String getStudenthobby() {
		return studentHobby;
	}
	public void setStudenthobby(String studenthobby) {
		this.studentHobby = studenthobby;
	}
	

}



Address.java 代码如下:

package com.tutorial.studentadmissioncontroller;

public class Address {
	
	private String country;
	private String city;
	private String street;
	private int pincode;
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	public int getPincode() {
		return pincode;
	}
	public void setPincode(int pincode) {
		this.pincode = pincode;
	}

}

StudentAdmissionController.java 代码如下

package com.tutorial.studentadmissioncontroller;

import org.springframework.stereotype.Controller;
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.RequestParam;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class StudentAdmissionController {

	@RequestMapping(value="/admissionForm.html", method = RequestMethod.GET)
	public ModelAndView getAdmission(){
		
		ModelAndView model = new ModelAndView("AdmissionForm");
		return model;
		
	}
	
	@RequestMapping(value="/submitAdmissionForm.html", method = RequestMethod.POST)
	public ModelAndView submitForm( @ModelAttribute("student") Student student, BindingResult result,
									@RequestParam(value="defaultvalue", defaultValue="this is a defaultvalue")String value){
		if(result.hasErrors()){
			ModelAndView model = new ModelAndView("AdmissionForm");
			System.out.println("There are some errors...");
			return model;
		}
		ModelAndView model = new ModelAndView("AdmissionSuccess");
		
		return model;
	
	}
}


解释:这个类是spring mvc 的核心, annotation 在这儿被使用. 

1. 当我们 运行该项目的时候, 我们首先要使用http://localhost:8080/SpringMVCTutorial/admissionForm.html    来访问AdmisionForm.jsp.  spring mvc framework会从该链接中提取 admissionForm.html然后到controller类中找相应的mapping. 发现 ok, 一个叫getAdmission()的方法匹配上了,那么接下来的动作都是在这个方法中.  进入该方法, 我们定义了一个ModelAndView 并且 传给他一个参数 AdmissionForm 并且 return.  这个return操作, 会把这个AdmissionForm 传递给spring-dispatcher-servlet.xml 中的,  然后 将会在  /WEB-INF/中找AdmissionForm.jsp,  最AdmissionForm.jsp被显示.


2. 当我们在AdmissionForm.jsp中填写好信息之后, 点击submit, 这个请求还会进行web.xml -> spring-dispatcher-servlet.xml - > StudentAdmissionController.java    因为在AdmissionForm.jsp中的action的链接为/SpringMVCtutorial/submitAdmissionForm.html    所以这次在StudentAdmissionController.java中寻找的就是submitAdmissionForm.html , 然后你可以对输入的数据进行操作( 对于spring是如何把从AdmissionForm.jsp中输入的信息封装到studnet类和address类的, 可以留言提问. 不在这里赘述).  最终返回到AdmissionSuccess.jsp 


AdmissionForm.jsp 代码如下:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>




AdmissionForm


STUDENT ADMISSION FORM FOR ENGINEERING COURSES

student name :

student Hobby :

student studentMobile :

student studentDOB :

student studentSkills :

default value :

Student address

country

city

street

pin


AdmissionSuccess.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>




Success


																 

this is the student details :

${student.studentName}

${student.studenthobby}

${student.studentMobile}

${student.studentDOB}

${student.studentSkills}

the student address

${student.address.country}

${student.address.city}

${student.address.street}

${student.address.pincode}


4. 使用 

http://localhost:8080/SpringMVCTutorial/admissionForm.html

进行测试

你可能感兴趣的:(JAVA,-,Spring,Framework)