SpringMVC案例应用

1.SpringMVC实现图片的上传

①引入commons-fileupload-1.3.1.jar和commons-io-2.4.jar

②applicationContext.xml文件中添加

<bean name="multipartResolver" 
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"></property>
</bean>
package com.yh.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @Author youHui
 **/
@Controller
@RequestMapping("/index")
public class FileController {
    @RequestMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("mFile") MultipartFile file){
        if(file != null){
            System.out.println("该文件大小是:"+file.getSize());
            File target = new File("D:\\workSpace\\Java\\SpringMVC\\MVC\\images\\upload.png");
            try {
                file.transferTo(target);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return "upload success";
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件上传</title>
</head>
<body>
<form action="/MVC_war_exploded/index/upload.form" method="post" enctype="multipart/form-data">
    <input type="file" name="mFile">
    <input type="submit" value="提交">
</form>
</body>
</html>

2.展示ModelAndView中的数据

①引入freemarker-2.3.20.jar

②配置FreeMarker标签库支持

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--在dispatcher-servlet.xml文件配置FreeMarker标签库支持-->
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value=""/>
        <property name="freemarkerSettings">
            <props>
                <prop key="tag_syntax">auto_detect</prop>
                <prop key="template_update_delay">1</prop>
                <prop key="defaultEncoding">UTF-8</prop>
                <prop key="url_escaping_charset">UTF-8</prop>
                <prop key="locale">zh_CN</prop>
                <prop key="boolean_format">true,false</prop>
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                <prop key="date_format">yyyy-MM-dd</prop>
                <prop key="time_format">HH:mm:ss</prop>
                <prop key="number_format">0.######</prop>
                <prop key="whitespace_stripping">true</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".html" />
        <property name="cache" value="true" />
        <property name="contentType" value="text/html; charset=UTF-8" />
        <property name="requestContextAttribute" value="request" />
    </bean>

</beans>

③代码自己建一个User类

package com.yh.mvc;

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

import java.util.ArrayList;
import java.util.List;

/**
 * @Author youHui
 **/
@Controller
@RequestMapping("/index")
public class ModelController {
    @RequestMapping("/testM")
    public ModelAndView getInfo(){
        ModelAndView mv = new ModelAndView("user");
        List<User> list = new ArrayList<>();
        for(int i=0;i<6;i++){
            User user = new User();
            user.setId(1+i);
            user.setName("admin"+i);
            user.setPwd("123456"+i);
            user.setAge(18);
            user.setAddress("昆明"+i+"号");
            list.add(user);
        }

        mv.addObject("list",list);
        return mv;
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h3>利用FreeMark获取用户列表</h3>
<div class="user_box">
    <table border="1" style="color:blue;">
        <tr>
            <th>ID</th><th>用户</th><th>密码</th><th>年龄</th><th>住址</th>
        </tr>
        <#list list as item>
        <tr>
            <td>${item.id}</td><td>${item.name}</td>
            <td>${item.pwd}</td><td>${item.age}</td><td>${item.address}</td>
        </tr>
        </#list>
    </table>

</div>
</body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值