SpringMVC入门

环境:jar包


1. SpringMVC主要组件:DispatcherServlet 、Controller 、ViewResolver 等

2. XML文件: web.xml 、[servlet-name]-servlet.xml

3. 执行流程:



4. SpringMVC之表单:


①web.xml 定义DispatcherServlet

<?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" 
		 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
		 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
		 id="WebApp_ID" version="2.5">
	
  <servlet>
    <servlet-name>FormHandling</servlet-name> <!-- 这里的名称一定要和[servlet-name]-servlet.xml一致 -->
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>FormHandling</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

② FormHandling-servlet.xml      定义<bean> 定义页面跳转的前缀后缀等-----------和-------------使用扫描来激活Controller,使注释生效

<?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">  
        
     <context:component-scan base-package="com.imut.po"/>
     <context:component-scan base-package="com.imut.controller"/>
     
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     	<property name="prefix" value="/WEB-INF/jsp/"/>
     	<property name="suffix" value=".jsp"/>
     </bean>
</beans>

③ 定义POJO 和Controller

public class Student {
	private int age;
	private String name;
	private int id;
	
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	public Student(int age, String name, int id) {
		super();
		this.age = age;
		this.name = name;
		this.id = id;
	}
	public Student() {
		super();
	}
	
	
	
}

@Controller
public class StudentController {
	
	@RequestMapping(value="/student",method = RequestMethod.GET)
	public ModelAndView student(){
		return new ModelAndView("student", "command", new Student());
		
	}
	
	@RequestMapping(value="/addStudent", method = RequestMethod.POST)
	public String addStudent(@ModelAttribute("studentForm")Student student , ModelMap model){
		model.addAttribute("name", student.getName());
		model.addAttribute("age",student.getAge());
		model.addAttribute("id", student.getId());
		return "result";
	}
	

}

注意:在 ModelAndView对象中传递了一个名为“ command”的空对象,因为如果在JSP中使用 <form:form>标签, spring框架需要一个名为“ command”的对象文件。 所以当调用 student()方法时,它返回 student.jsp视图。

④student.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!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=utf-8">
<title>Spring 处理表单</title>
</head>
<body>
<h2>Student Information</h2>
<form:form action="/FormHanding/addStudent" method="POST" >
	<table>
		<tr>
			<td><form:label path="name">名字:</form:label></td>
			<td><form:input path="name"/></td>
		</tr>
		<tr>
			<td><form:label path="age">年龄:</form:label></td>
			<td><form:input path="age"/></td>
		</tr>
		<tr>
			<td><form:label path="id">编号</form:label></td>
			<td><form:input path="id"/></td>
		</tr>
		
		<tr>
			<td colspan="2">
				<input type="submit" value="提交表单"/>
			</td>
		</tr>
	</table>
</form:form>
</body>
</html>

result.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!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=utf-8">
<title>Spring MVC表单处理</title>
</head>
<body>
<h2>提交的学生信息如下</h2>
<table>
	<tr>
		<td>姓名</td>
		<td>${name }</td>
	</tr>
	<tr>
		<td>年龄</td>
		<td>${age }</td>
	</tr>
	<tr>
		<td>编号</td>
		<td>${id }</td>
	</tr>
</table>
</body>
</html>


    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值