springmvc

springmvc注解开发

项目搭建:

引入类库jar

com.springsource.javax.servlet.jsp.jstl-1.1.2.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.taglibs.standard-1.1.2.jar
org.springframework.aop-3.0.0.RELEASE.jar
org.springframework.asm-3.0.0.RELEASE.jar
org.springframework.beans-3.0.0.RELEASE.jar
org.springframework.context.support-3.0.0.RELEASE.jar
org.springframework.context-3.0.0.RELEASE.jar
org.springframework.core-3.0.0.RELEASE.jar
org.springframework.expression-3.0.0.RELEASE.jar
org.springframework.web.servlet-3.0.0.RELEASE.jar
org.springframework.web-3.0.0.RELEASE.jar

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" 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>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<!-- struts用/*, springmvc不能/*,  语法 *.xxx -->
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>
<p></web-app>
</p>

springmvc-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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-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/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
		
		<!-- 	注解驱动  -->
		<!-- <mvc:annotation-driven/> -->
								
		<!-- 注解扫描器 包含注解驱动 -->
		<context:component-scan base-package="com.taxue.controller"/>
		
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix" value="/WEB-INF/jsp/"></property>
			<property name="suffix" value=".jsp"></property>
		</bean>
	
</beans>

TestController.java

package com.taxue.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class TestController {
	/**
	 * author:王乾坤
	 * mail:651518854@qq.com
	 */
	@RequestMapping("/index.html")
	public String index(){
		return "index";
	}

}





文件上传

springmvc-servlet.xml

<!--文件上传  -->
		<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
			<property name="maxUploadSize" value="1024000"></property>
		</bean>

UploadController.java

package com.taxue.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.mail.Multipart;
import javax.resource.spi.Connector;
import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.taxue.model.Person;

@Connector
@RequestMapping("/upload")
public class UploadController {

	/**
	 * 
	 * 上传
	 * author:王乾坤
	 * mail:651518854@qq.com
	 */
	@RequestMapping("/uploadPic.html")
	public String uploadPic(Person person,HttpServletRequest request) throws IOException{
		MultipartHttpServletRequest mr = (MultipartHttpServletRequest) request;
		//获得文件
		MultipartFile mf = mr.getFile("pic");
		//获得文件的字节数组
		byte[] fbyte = mf.getBytes();
		String filename = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
		Random random = new Random();
		for(int i = 0 ; i < 3 ;i++){
			filename = filename + random.nextInt(10);
		}
		//获得原始文件名
		String oriFileName = mf.getOriginalFilename();
		// 获得后缀名
		String suffix =oriFileName.substring(oriFileName.lastIndexOf("."));
		//拿到服务器的根目录
		String realPath = request.getSession().getServletContext().getRealPath("/");
		OutputStream out = new FileOutputStream(new File(realPath +"/upload/"+filename+suffix));
		out.write(fbyte);
		out.flush();
		out.close();
		return "success";
	}
	@InitBinder
	public void initBinder(ServletRequestDataBinder binder){
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(), true));
	}
	@RequestMapping("/index.html")
	public String index() {
		System.out.println("ok");
		return "admin/form";
	}
}

form.jsp


  <body>
	<form action="upload/uploadPic.html" method="post" enctype="multipart/form-data">
		id:<input name="id" type="text"><br> 
		name:<input name="name" type="text"><br> 
		age:<input name="age" type="text"><br> 
		pic:<input name="pic" type="file"><br>
		<input value="submit" type="button">
	</form>
</body>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值