ssm后端校验,解决数据无法回显和jsp页面属性无法绑定userBean

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<jsp:useBean id="user" class="com.sjxy.ssm.entity.User"></jsp:useBean>   
<!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>Insert title here</title>

</head>
<body>
<center>
<a href="${pageContext.request.contextPath}/emp/list">显示所有员工信息</a>
<form:form modelAttribute="user" action="${pageContext.request.contextPath}/user/upload" enctype="multipart/form-data" method="post">
  <table>
    <tr>
    <td>Email address</td>
     <td><input name="email"/></td>
    <td><form:errors path="email" cssStyle="color:red"/></td>
    </tr>
    <tr>
    <td>Password</td>
    <td><input type="password" name="password"/></td>
    <td><form:errors path="password" cssStyle="color:red"/></td>
    <tr>
    <td>File input <input type="file" name="image" ></td>
    <td><input type="checkbox">
     Check me out</td>
    <td><button type="submit">提交</button></td>
    </tr>
  </table>
</form:form>
</center>
</body>
</html>
首先导入useBean
<jsp:useBean id="user" class="com.sjxy.ssm.entity.User"></jsp:useBean> 
数据绑定,modelAttribute=”user”最好要写
<form:form modelAttribute="user" action="${pageContext.request.contextPath}/user/upload" enctype="multipart/form-data" method="post">
数据回显,input最好不要用 <form:input> 老是报错,不清楚具体原因。
<td>Email address</td>
<td><input name="email"/></td>
<td><form:errors path="email" cssStyle="color:red"/></td>
Controller做的上传下载register.java
package com.sjxy.ssm.web.hander;

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

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sjxy.ssm.entity.User;
@Controller
@RequestMapping(value="/user")
public class register {

    @RequestMapping(value="/upload")
    public String upload(@Valid @ModelAttribute("user") User user,Errors errors,
            Model model,HttpServletRequest request) throws Exception  {
        System.out.println(user.getEmail());
        //logger.info(user);

        if(errors.hasErrors()){
            return "user/login";
        }


        if(!user.getImage().isEmpty()){
            String path="E:/upload";
            System.out.println(path);
            String filename=user.getImage().getOriginalFilename();
            File filepath=new File(path,filename);
            if(!filepath.getParentFile().exists()){
                filepath.getParentFile().mkdirs();
            }
            user.getImage().transferTo(new File(path+File.separator+filename));
            model.addAttribute("user", user);
            return "user/success";
        }
        return "user/error";
    }
    @RequestMapping(value="/download")
    public ResponseEntity<byte[]> download(HttpServletRequest request,
            @RequestParam("filename") String filename,Model medel) throws Exception{
        String path="E:/upload";
        System.out.println(filename);
        File file=new File(path+File.separator+filename);
        HttpHeaders headers=new HttpHeaders();
        String downloadFileName=new String(filename.getBytes("UTF-8"),"iso-8859-1");
        headers.setContentDispositionFormData("attachment", downloadFileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers,HttpStatus.CREATED);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值