spring mvc annonation(环境搭建和最基本的请求映射)

本文详细介绍了一个基于Spring MVC的简单学生信息管理系统实现过程,包括配置web.xml和spring-servlet.xml,创建Student类及Controller,处理表单提交等。通过具体代码示例展示了如何搭建Spring MVC项目并实现基本的CRUD操作。

 1.配置web.xml
2.配置xxxx-servlet.xml--支持部件扫描和视图解析
3.创建一个学生类Student,一个Controller,名称是StudentController,注释为@Controller
4.添加工程需要的jar包,将spring3.0所有jar放进去,commons-logging.jar,log4j-1.2.15.jar,commons-fileupload-1.2.jar,反正就是根据(启动tomcat时)错误提示增加jar
5.创建一个index.jsp,加一个链接到studentAdd
6.在Controller中处理studentAdd
7.在edit.jsp中使用spring标签库
 <1>在jsp中增加(第一行是处理编码)
 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
 <%@ taglib prefix="form" uri="http://www.springframe.work.org/tags/form" %>
 <2>在web.xml中配置标签库位置
 <jsp-config>
  <taglib>
   <taglib-uri>http://www.springframe.work.org/tags/form</taglib-uri>
   <taglib-location>/WEB-INF/tld/spring-form.tld</taglib-location>
  </taglib>
 </jsp-config>
 <3>将spring-form.tld放入相应文件夹
-------------------------------插入@ModelAttribute标签使用-----------------------
1>注释在Controller中,标注在方法上,这个方法会在所有标注@RequestMapping的方法前被执行,一般用来生成对象放在model中,如果不定义名字,默认的名字是类名,但第一个字母小写
2>注释在@RequestMapping的方法的参数的类型前面,会将表单parameter根据名字注入到参数中(字符串映射到其他类型需要convert),如果没有响应的参数,则会new一个新的对象
-----------------------------------------------------------------------------------------------

代码:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
	version="2.4">

	<display-name>springmvc</display-name>
	<description>
     sms admin site
  	</description>
  	
	<context-param>
		<param-name>webAppRootKey</param-name>
		<param-value>backend.root</param-value>
	</context-param>
	
	
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>classpath:config/log4j.properties</param-value>
	</context-param>
	<listener>
		<listener-class>
			org.springframework.web.util.Log4jConfigListener
		</listener-class>
	</listener>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:config/context/applicationContext*.xml
		</param-value>
	</context-param>
	
	
	<servlet>
		<servlet-name>study</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>			

	<servlet-mapping>
		<servlet-name>study</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<session-config>
		<session-timeout>20</session-timeout>
	</session-config>

	<welcome-file-list>
		<welcome-file>index.do</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
	</welcome-file-list>
 
 	<jsp-config>
		<taglib>
			<taglib-uri>http://www.springframe.work.org/tags/form</taglib-uri>
			<taglib-location>/WEB-INF/tld/spring-form.tld</taglib-location>
		</taglib>
	</jsp-config>
</web-app>


study-servlet.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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
        
    <context:component-scan base-package="com.smvc"/>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
        p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
</beans>


Student.java

package com.smvc.annonation.bean;

import java.util.Date;

public class Student {
	
	private String name;
	private String gender;
	private int age;
	private Date birthday;
	
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	
}


StudentController.java

package com.smvc.annonation.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.servlet.ModelAndView;

import com.smvc.annonation.bean.Student;

@Controller
public class StudentController {
	protected final Log logger = LogFactory.getLog(getClass());
	/*
	 * 会将一个新的Student增加到modelattribute中,名字为st2,在视图中可以使用,也许可以用在编辑页面
	@ModelAttribute("st2")
	public Student getStudent(){
		System.out.println("In modelAttribute");
		Student re = new Student();
		re.setName("Haha");
		re.setAge(30);
		re.setGender("male");
		return re;
	}
	*/
	@RequestMapping(value="/studentAdd")
	public ModelAndView studentAdd(@ModelAttribute("bb") Student cc, BindingResult br){
			if(br.hasErrors()) {
				return new ModelAndView("edit");
			}
			logger.info(br);
			System.out.println(cc.getName());
			return new ModelAndView("aa");
	}
}


log4j.properties

log4j.rootLogger=warn, stdout

#stdout console appender
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{yyyy.MM.dd HH:mm:ss}] %p %C:%M(%L) - %m%n


index.jsp

<a href="studentAdd">添加学生</a>


edit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" %>
<%@ taglib prefix="form" uri="http://www.springframe.work.org/tags/form" %>
<style>
	.error { color: red; }
</style>
<html>
<head>
<title>学生管理系统</title>
</head>
<body>
	<form:form action="edit" method="post" commandName="bb">
		<table>
			<tr><td align="center">学生姓名:</td><td><form:input path="name"/></td><td><form:errors path="name" cssClass="error"/></td></tr>
			<tr><td align="center">性别:</td><td><form:input path="gender" /></td><td><form:errors path="gender" cssClass="error"/></td></tr>
			<tr><td align="center">年龄:</td><td><form:input path="age"/></td><td><form:errors path="age" cssClass="error"/></td><tr/>
			<tr><td align="center">生日:</td><td><form:input path="birthday"/></td><td><form:errors path="birthday" cssClass="error"/></td><tr/>
			<tr><td align="center"><button type="submit">提交</button></td></tr>
		</table>
	</form:form>
</body>
</html>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值