⽂件上传、数据校验(后台)、
1. ⽂件上传
⽬录
1. 需要上传的jar包——fileploat
2. ⽹页端
表单必须是post提交,编码必须是multipart/form-data ⽂件上传⽂本框必须起名
Choose File提交
上传⽂件No file selected
```
3. 在springMVC中配置⽂件上传解析器
```
4. 在控制层处理代码
package com.ller;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.apachemons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.wlq.bean.Student;
@Controller
public class StudentController {
@RequestMapping("stuUpload")
public String addStudent(MultipartFile photo,Student stu,HttpServletRequest request,Model model) {
//获取⽂件上传的真实保存路径
String path = ServletContext().getRealPath("/photo");
File file = new File(path);
if(!ists()) {
file.mkdirs();
}
String filename = System.currentTimeMillis()+OriginalFilename();
stu.setSphoto("photo/"+filename);
File targetFile = new File(path+"/"+filename);
try {
FileUtils.writeByteArrayToFile(targetFile, Bytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
model.addAttribute("stu",stu);
return "index";
}
}
```
5. Student 实体类spring framework jar包
package com.alibaba.wlq.bean;
public class Student {
private String name;
private Integer age;
//注意:Student实体类中图⽚的属性名称不能和⽂件上传中名⾂属性的值⼀致,否则会出现400错误
private String sphoto;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSphoto() {
return sphoto;
}
public void setSphoto(String sphoto) {
this.sphoto = sphoto;
}
}
```
2.
什么是?
Spring MVC中的(Interceptor)类似于Servlet中的过滤器(Filter),它主要⽤于拦截⽤户请求并作相应的处理。例如通过可以进⾏权限验证、记录请求信息的⽇志、判断⽤户是否登录等。
要使⽤Spring MVC中的,就需要对类进⾏定义和配置。
1. 创建⼀个类,实现HandlerInterceptor接⼝并且重写接⼝中的⽅法
package com.alibaba.wlq.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class Interceptor implements HandlerInterceptor{
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
Object user = Session().getAttribute("user");
if(user!=null) {
return true;
}else {
response.sendRedirect("toLogin");
return false;
}
}
}
```
2. 把创建的类配置到SpringMVC的配置⽂件中
<mvc:interceptors>
<mvc:interceptor>
<!--表⽰拦截所有user下以及user下⼦类的请求-->
<mvc:mapping path="/user/**"/>
<!---->
<mvc:exclude-mapping path="/user/toLogin"/>
<mvc:exclude-mapping path="/user/login"/>
<mvc:exclude-mapping path="/user/toRegister"/>
<mvc:exclude-mapping path="/user/register"/>
<mvc:exclude-mapping path="/user/index"/>
<bean class="com.alibaba.wlq.interceptor.Interceptor"/>
</mvc:interceptor>
</mvc:interceptors>
```
3.数据校验(后台校验)
1. 导⼊jar包(validate)
2. 在相应的实体类中加⼊注解
package com.alibaba.wlq.bean;
import straints.NotNull;
import straints.Pattern;
import org.straints.Length;
import org.straints.NotEmpty;
public class User {
@NotEmpty(message="账号不能为空")
@NotNull(message="账号不能为空")
private String account;
@NotEmpty(message="密码不能为空")
@NotNull(message="密码不能为空")
@Length(min=6,max=12,message="密码的长度在6~12之间")
private String password;
@Pattern(regexp="/^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/",message="⼿机号码格式不正确") private String phone;
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public User(String account, String password, String phone) {
super();
this.account = account;
this.password = password;
this.phone = phone;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(String account, String password) {
super();
this.account = account;
this.password = password;
}
public User() {
super();
}
}
3. 控制层接受参数时
@RequestMapping("register")
public String register(@Valid User user,BindingResult br,Model model) {
if(br.hasErrors()) {
List fieldList = br.getFieldErrors();
Map<String,Object> errorMsg = new HashMap<>();
for (FieldError f : fieldList) {
errorMsg.Field(), f.getDefaultMessage());
}
model.addAttribute("errorMsg",errorMsg);
return "register";
}
return "login";
}
```
4. 在⽹页中拿到错误信息
<body> <form action="register" method="post"> 账号:<input type="text" name="account"/>${errorMsg.account }<br> 密码:<input type="text" name="password"/>${errorMsg.password }<br> ⼿机号:<input type="text" name="phone"/>${errorMsg.phone}<br> <input type="submit" value="提交"/> </form> </body> ```
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论