参考:https://www.cnblogs.com/goody9807/p/6450589.html
配置文件不多说,上一篇文章已经写过。
1.导入jar包
2.在WEB-INF文件夹下创建jsp文件夹,用来存放视图层jsp文件,另在根目录创建index.jsp(我在其他目录创建index总报错,修改貌似很麻烦,所以index以后就默认在根目录)。
今天大概目录如下:
3.首先是在com.SpingMVC下创建mvcController.java文件,如下
@Controller //用来定义Controller类
@RequestMapping("/mvc") //URL映射
public class mvcController {
@RequestMapping("/hello") //URL映射
public String hello(){
return "hello"; //返回为jsp文件,即视图层
}
即当url为 http://localhost:8080/mvc/hello 时,返回hello.jsp
4.实现自动装箱,Person类不用实例化
@RequestMapping("/person1")
public String woPerson(Person p) { //直接引用Person类
System.out.println(p.getName()+" "+p.getAge());
return "hello";
}
5.使用ajax调用
@RequestMapping("/getPerson")
public void getPerson(String name,PrintWriter pw){
pw.write("hello,"+name);
}
@RequestMapping("/name")
public String sayHello(){
return "name";
}
前台代码:
ajax调用:
<input id="name" type="text">
<input id="btn" type="button" value="提交"><br><br><hr>
JQuery代码:
<script type="text/javascript" src="./public/jq/jquery-3.3.1.min.js"> </script>
<script type="text/javascript">
$("document").ready(function(){
$("#btn").click(function(){
$.post("mvc/getPerson",{name:$("#name").val()},function(data){
alert(data);
})
});
});
</script>
6.使用redirect请求
@RequestMapping("/redirect")
public String redirect(){
return "redirect:hello";
}
7.文件上传
在SpringMVC配置文件内加入:
<!-- 文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="102400000"></property>
</bean>
controller代码:
@RequestMapping(value="/upload",method=RequestMethod.POST)
public String upload(HttpServletRequest req) throws Exception{
MultipartHttpServletRequest mreq = (MultipartHttpServletRequest) req;
MultipartFile file = mreq.getFile("file");
String fileName = file.getOriginalFilename();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//用文件输出流将上传文件写入
FileOutputStream fos = new FileOutputStream(req.getSession().getServletContext().getRealPath("/")+
"upload/"+sdf.format(new Date())+fileName.substring(fileName.lastIndexOf('.')));
fos.write(file.getBytes());
fos.flush();
fos.close();
return "hello";
}
前台代码:
文件上传:
<form action="mvc/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="上传">
</form><br><br><hr>
8.form表单发送四种请求
在web.xml中添加如下配置:
<!-- configure the HiddenHttpMethodFilter,convert the post method to put or delete -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
前台代码:
from表单发送put和delete请求:
<form action="rest/user/1" method="post">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="put">
</form>
<form action="rest/user/1" method="post">
<input type="submit" value="post">
</form>
<form action="rest/user/1" method="get">
<input type="submit" value="get">
</form>
<form action="rest/user/1" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="delete">
</form><br><br><hr>