SpringMVC学习笔记(二)

SpringMVC学习笔记(二)

综合示例,文件上传,自定义拦截器,json交互

个人笔记,如有错误,恳请批评指正。

综合示例(springmvc文件上传)

multipartResolver使用

spring-mvc.xml文件添加如下内容:

<!--文件上传使用, 配置multipartResolver,id名为约定好的 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 配置文件(每次上传的所有文件总大小)大小,单位为b, 1024000表示1000kb  -->
    <property name="maxUploadSize" value="1024000" />       
</bean>
中文乱码处理

web.xml文件添加如下内容:

<filter>
    <filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

如果上边的方式设置后,仍然有乱码,请尝试修改tomcat安装目录下的apache-tomcat安装目录\conf\server.xml文件,修改Connector元素内容,添加URIEncoding="UTF-8" ,修改后内容 如下:

<Connector port="8080" protocol="HTTP/1.1" 
            connectionTimeout="20000" 
              redirectPort="8443" URIEncoding="UTF-8"/>
properties文件信息注入

PropertiesFactoryBean:用来注入properties类型的配置文件信息

<!--PropertiesFactoryBean对properties文件可用 ,可以用来注入properties配置文件的信息 -->
<bean id="uploadProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="location" value="classpath:xxxxx.properties"></property>
</bean>
文件上传示例
导入包

继续使用上一章节代码,并导入文件上传需要的jar包:commons-fileupload-1.2.2.jar, commons-io-2.0.1.jar

修改student实体类,添加文件类型属性
public class Student implements Serializable {

    private static final long serialVersionUID = -5304386891883937131L;
    private Integer stuId;
    private String stuName;
    private String stuPwd;
    private Integer stuAge;
    private MultipartFile[] files;
    ......
}
编写上传页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
  </head>

  <body>     
     <form action="student/save.action" method="post" enctype="multipart/form-data">
        姓名:<input type="text" name="stuName"><br/>
        密码<input type="password" name="stuPwd"><br>
        请选择文件:<br/><input type="file" name="files"><br/>
        <input type="file" name="files"><br/>
        <input type="submit" value="文件上传测试">    
     </form>    
  </body>
</html>
编写控制器

StudentAction.java

@Controller
@RequestMapping("/student")
public class StudentAction {

    public StudentAction(){
        System.out.println("---StudentAction构造方法被调用---");
    }
    @RequestMapping("/save")
    public String save(Student student) {
        System.out.println("save方法已注入student对象:"+student);
        MultipartFile[] files=student.getFiles();
        for(MultipartFile file:files){
            if(file.isEmpty()){
                System.out.println("文件为空");
            }else{
                System.out.println("文件不为空!");
                System.out.println("格式:" + file.getContentType());
                System.out.println("原名:" + file.getOriginalFilename());
                System.out.println("大小:" + file.getSize());
                System.out.println("表单控件的名称" + file.getName());
                try {
                    FileUtils.copyInputStreamToFile(file.getInputStream(), new File("e:/testupload/"+file.getOriginalFilename()));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }               
            }
        }   
        System.out.println("---调用业务逻辑进行业务处理---");       
        //直接使用字符串,返回视图,进行结果展现等
        return "forward:/jsp/main.jsp";
    }
}
修改配置文件

添加文件处理器CommonsMultipartResolver配置

<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    ">
    <mvc:annotation-driven></mvc:annotation-driven>
    <context:component-scan base-package="*"/>
    <!--文件上传使用, 配置multipartResolver,id名称为约定好的 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 配置文件(每次上传的所有文件总大小)大小,单位为b, 1024000表示1000kb  -->
        <property name="maxUploadSize" value="1024000" />       
    </bean>
</beans>
编写处理完后跳转的页面

main.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
      <title>main.jsp</title>   
  </head>

  <body>
    /jsp/main.jsp页面
          student: ${requestScope.student}
  </body>
</html>
文件存放于tomcat目录下处理方式

 在项目目录下新建upload文件夹

 修改StudentAction.java。

@Controller
@RequestMapping("/student")
public class StudentAction {

    public StudentAction(){
        System.out.println("---StudentAction构造方法被调用---");
    }

    @Resource
    ServletContext application;

    @RequestMapping("/save")
    public String save(Student student) {
        System.out.println("save方法已注入student对象:"+student);
        MultipartFile[] files=student.getFiles();
        System.out.println("真实路径:"+application.getRealPath("/"));
        for(MultipartFile file:files){
            if(file.isEmpty()){
                System.out.println("文件为空");
            }else{
                System.out.println("文件不为空!");
                System.out.println("格式:" + file.getContentType());
                System.out.println("原名:" + file.getOriginalFilename());
                System.out.println("大小:" + file.getSize());
                System.out.println("表单控件的名称" + file.getName());

                try {
                    FileUtils.copyInputStreamToFile(file.getInputStream(), new File(application.getRealPath("/")+"upload/"+file.getOriginalFilename()));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }               
            }
        }   
        System.out.println("---调用业务逻辑进行业务处理---");       
        //直接使用字符串,返回视图,进行结果展现等
        return "forward:/jsp/main.jsp";
    }
}

其它代码同上一章节,可以在application.getRealPath(“/”)+”upload/”目录下查看到文件。

文件上传优化
编写文件上传工具类

FileUploadUtil.java

@Component(value="fileUploadUtils")  //普通的bean注入
public class FileUploadUtils {
    /*
     * 注入字符串,#{}为spel语言,其中uploadProperties,是xml配置文件中注入properties文件的bean id,
     *  path为properties文件的其中一个key ,也可以通过下边的set方法注入
     */

    @Value("#{uploadProperties.path}") 
    private String path;
    //private String path="e:/testupload";

    //path也可以通过set方法注入
//  @Value("#{uploadProperties.path}") 
//  public void setPath(String path) {
//      this.path = path;
//  }   

    private String getExtName(MultipartFile file){
        return FilenameUtils.getExtension(file.getOriginalFilename());
    }

    private String createNewName(MultipartFile file){
        return UUID.randomUUID().toString()+"."+getExtName(file);
    }

    public String uploadFile(MultipartFile file){
        try {
            String newName=createNewName(file);
            FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path,newName ));
            return newName;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new RuntimeException(e);          
        }       
    }
}
修改StudentAction.java

主要修改save方法,使用自已的文件上传工具类进行文件上传。

@Controller
@RequestMapping(value="/student")
public class StudentAction {

    @Resource
    private ServletContext application;

    @Resource
    private FileUploadUtils fileUploadUtils;

    public StudentAction(){
        System.out.println("---StudentAction构造方法被调用---");
    }

    @RequestMapping(value="/save")
    public String save(Student student,Map<String, Student> paramMap) {
        System.out.println("save方法已注入student对象:"+student);
        MultipartFile[] files=student.getFiles();
        for(MultipartFile file:files){
            if(file.isEmpty()){
                System.out.println("文件为空");
            }else{
                System.out.println("文件不为空!");
                fileUploadUtils.uploadFile(file);
            }
        }   
        System.out.println("---调用业务逻辑进行业务处理---");       
        paramMap.put("student",student);        
        //直接使用字符串,返回视图,进行结果展现等
        return "forward:/jsp/main.jsp";
    }
}
添加upload.properties文件

配置文件上传后的存放目录

path=e\:\\testdir\\upload\\
修改spring-mvc.xml配置文件

注入配置文件的信息

<!--PropertiesFactoryBean对properties文件可用 ,可以用来注入properties配置文件的信息 -->
<bean id="uploadProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="location" value="classpath:upload.properties"></property>
</bean>

综合示例(登陆)

拦截器使用
编写拦截器类

LoginInterceptor.java,需要实现HandlerInterceptor接口

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public void afterCompletion(HttpServletRequest arg0,
            HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
        // TODO Auto-generated method stub
        System.out.println("---访问请求资源后不管理有没有异常都一定执行此方法---");

    }
    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2, ModelAndView arg3) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("---访问请求资源后,如果没有异常,将执行此方法---");
    }
    @Override
    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("---访问请求资源前执行,如果此方法返回false,将不能访问请求资源---");
        return true;
    }
}
配置文件中添加拦截器
<!-- 配置spring mvc拦截器 -->
<mvc:interceptors>
<!-- 默认拦截DispatcherServlet指定的后缀(这里是.action) -->
    <bean class="cn.itcast.interceptor.LoginInterceptor"/>
</mvc:interceptors>
登陆示例
编写及配置拦截器

添加拦截器类及拦截器配置信息,如上面。

修改拦截器类preHandle方法
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
        Object arg2) throws Exception {
    // TODO Auto-generated method stub
    System.out.println("---访问请求资源前执行,如果此方法返回false,将不能访问请求资源---");
    if(arg0.getSession().getAttribute("user")==null){
        arg1.sendRedirect(arg0.getContextPath()+"/login.jsp");
        return false;
    }
    return true;
}
编写登陆页面

login.jsp,本页面已模仿了登陆

<%@page import="cn.itcast.entity.Student"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
  </head>
  <body>  
    <%
        session.setAttribute("user", new  Student(1001,"zcf","admin",20));
     %>  
     <!-- 这里正常应该跳到action再到页面 ,为了演示,这里简略--> 
     <a href="index.jsp">已登陆,返回首页</a> 
  </body>
</html>

json交互

使用上面的源码,暂时去掉拦截器的登陆权限处理

导入json包及jquery的js文件

修改action文件

@Controller
@RequestMapping(value="/student")
public class StudentAction {

    public StudentAction(){
        System.out.println("---StudentAction构造方法被调用---");
    }
    @RequestMapping("/doAjax")
    @ResponseBody //如果返回json格式,需要这个注解
    public Object doAjax(Student student){
        System.out.println("---doAjax.student:"+student);
        student.setStuName("1001name");
        return student;
    }
}
修改访问页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<%
    pageContext.setAttribute("basePath", request.getContextPath() + "/");
%>
<title></title>
</head>
<script type="text/javascript" src="${basePath }js/jquery/jquery-1.3.js"></script>
<script type="text/javascript">
    $(function() {
        $("#login").click(function() {
            $.ajax({
                url : "student/doAjax.action",
                type : "post",
                dataType : "json",
                data : {
                    "stuName" : "sss",
                    "stuPwd" : "sss"
                },
                success : function(ajax) {
                    alert(ajax.stuName + ajax.stuPwd);
                }
            });
        });
    });
    $(function(){
            $("#bt1").click(
                function(){
                $.post(
                    "student/doAjax.action",
                    {stuName:"name1001",stuPwd:"pwd1001"},
                    function(json){alert(json.stuName+"||"+json.stuPwd);},
                    "json"
                );
            }
        );              
        }
    );


</script>
<body>
    <button id="login">testajax</button>
    <button id="bt1">testajax</button>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值