logo头像

一路过来,不过游牧自己。。

SSM之SpringMVC(五)


话说我们这四个步骤都基本处理完了,那么接下来就是要去处理怎么用了,但是这过程还是有很多零散的问题要解决,所以这节我们就来会会他们!

6、乱码及Restful

(1)乱码的解决

是这样,如果传过来的参数是英文是没啥问题的,但如果是中文呢,就会出现中文乱码的问题,所以如何去解决呢?
这就需要过滤器去解决乱码:SpringMvc中提供CharacterEncodingfilter解决一个POST乱码
在web.xml中加上如下代码

1
2
3
4
5
6
7
8
9
10
11
12
<filter>
<filter-name>encoding</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

如果是get方式乱码
a)修改tomcatd的配置方式解决
b)自定义乱码解决的过滤器

(2)restful风格的url(是一个映射对比的过程)

优点:轻量级,安全,效率高

1
2
3
4
5
/http://localhost:8080/restful/1/123/delete.do
@RequestMapping("/{id}/{uid}/hello.do")
public void helloRestful(@PathVariable int uid,@PathVariable("id") int id){
System.out.println("id=="+id);
}

(3)同一个Controller通过参数来达到不同的处理方法–不重要

提交的url:
/http://localhost:8080/restful/hello2.do?method=add
处理代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Controller
@RequestMapping("/hello2")
public class Hello2Controller {
@RequestMapping(params="method=add")
public String add(){
System.out.println("add");
return "redirect:index.jsp";
}
public String update(){
System.out.println("update");
return "redirect:index.jsp";
}
public String delete(){
System.out.println("delete");
return "redirect:index.jsp";
}
}

7、SpringMVC实现文件上传

(1)通过commons-fileupload来实现,导入相关jar包

commons-fileupload,commons-io

(2)配置springmvc的配置解析器

(3)jsp页面

1
2
3
<form action="upload.do" enctype="multipart/form-data" method="post">
文件1:<input type="file" name="file"/><input type="submit" value="上传"/>
</form>

(4)Controller代码

@RequestMapping(“/upload.do”)
public String upload(@RequestParam(“file”) CommonsMultipartFile file,HttpServletRequest req){
//获取上传文件路径
String path=req.getRealPath(“/upload”);
//获取文件名
String filename=file.getOriginalFilename();
try {
InputStream is = file.getInputStream();
OutputStream os = new FileOutputStream(new File(path,filename));
int len=0;
byte[] buffer = new byte[400];
while((len=is.read(buffer))!=-1){
os.write(buffer, 0, len);
}
os.close();
is.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return "redirect:index.jsp";
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
这里@RequestParam("file")必须要写!!
批量上传的代码:
```java
@RequestMapping("/batch.do")
public String batch(@RequestParam("file") CommonsMultipartFile file[],HttpServletRequest req){
//获取上传文件路径
String path=req.getRealPath("/upload");
for(int i=0;i<file.length;i++){
//获取文件名
String filename=file[i].getOriginalFilename();
try {
InputStream is = file[i].getInputStream();
OutputStream os = new FileOutputStream(new File(path,filename));
int len=0;
byte[] buffer = new byte[400];
while((len=is.read(buffer))!=-1){
os.write(buffer, 0, len);
}
os.close();
is.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "redirect:index.jsp";
}

好了,就这样了!

微信打赏

赞赏是不耍流氓的鼓励