您的位置:首页 > 编程语言 > Java开发

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

2016-06-07 04:12 429 查看
简单明了的Spring MVC 基础教学. 

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

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


 

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

3.  web.xml 代码如下:

<?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>SpringMVCTutorial</display-name>
<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>

<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app


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

sping-dispatcher.xml  代码如下:

<?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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
<context:component-scan base-package="com.tutorial.studentadmissioncontroller"/>

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

</beans>


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

<context:component-scan base-package="...">  这行代码, 告诉spring mvc 我们的controller 类到包含在这个包里面了, 想要对从web.xml里面来的那个请求进行处理,就从这个包里面找好了. 
<bean> ... </bean>这个里面包含的所有东西, 就有一个目的就是 对 从 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<String> 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<String> getStudentSkills() {
return studentSkills;
}
public void setStudentSkills(ArrayList<String> 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 中的<bean>,  然后<bean> 将会在  /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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>AdmissionForm</title>
</head>
<body>
<h1>STUDENT ADMISSION FORM FOR ENGINEERING COURSES</h1>

<form action="/SpringMVCtutorial/submitAdmissionForm.html" method="POST">
<p>
student name : <input type="text" name="studentName"/>
</p>

<p>
student Hobby : <input type="text" name="studentHobby" />
</p>

<p>
student studentMobile : <input type="text" name="studentMobile" />
</p>
<p>
student studentDOB : <input type="text" name="studentDOB" />
</p>
<p>
student studentSkills : <input type="text" name="studentSkills" />
</p>
<p>
default value : <input type="text" name="defaultvalue">
</p>

<h3>Student address</h3>
<p>country <input type="text" name="address.country"/>	</p>
<p>city <input type="text" name="address.city"/>	</p>
<p>street <input type="text" name="address.street"/></p>
<p>pin <input type="text" name="address.pincode" /></p>

<input type="submit" value="Submit">

</form>
</body>
</html>


AdmissionSuccess.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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Success</title>
</head>
<body>
</h1>
<h2>this is the student details : </h2>
<p> ${student.studentName} </p>
<p> ${student.studenthobby}</p>
<p> ${student.studentMobile}</p>
<p> ${student.studentDOB}</p>
<p> ${student.studentSkills}</p>
<h3>the student address</h3>
<p>${student.address.country}</p>
<p>${student.address.city}</p>
<p>${student.address.street}</p>
<p>${student.address.pincode}</p>
</body>
</html>


4. 使用  http://localhost:8080/SpringMVCTutorial/admissionForm.html
进行测试
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: