SpringMVC
1.需要的 jar 文件
2.在 web.xml 中配置 Springmvc 拦截器
<!-- SpringMVC 配置 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3.添加 SpringMVC 配置文件
<!-- 使用下面代码使 ctrl 完成依赖注入的功能 -->
<context:annotation-config />
<context:component-scan base-package="com.wsy.test.ctrl"/>
<mvc:default-servlet-handler />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 添加 json 支持 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<!-- 文件上传支持 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="102400000"></property>
</bean>
4.Springmvc 的 ctrl (控制层)的书写
控制层的主要功能就是处理前台发送过来的请求,而请求分为,同步请求,异步请求
同步请求,又分为带参数和不带参数
带参数就涉及到数据怎么在ctrl 中获取 (一下方法都是将数据作为方法参数传入)
1.数据绑定 @RequestParam("name")String name 将页面中的 那么属性的值绑定到 name 变量中
2使用 Httpservletrequest 对象获取前台传递过来的数据
3.使用 对象模型的自动封装 ,但对象模型的属性名字必须和前台页面的属性名字一致
同步请求,不带参数,方法不带参数即可
jsp 代码如下:
获取页面传递的参数:
<a href="<%=request.getContextPath()%>/test/test1?args1=wsy"> ctrl 获取页面传递的参数 </a>
ctrl 代码如下:
@RequestMapping("/test1")
public String test1(@RequestParam("args1") String arg){
System.out.println("页面传递的参数:"+arg);
return "test1";
}
异步请求 (涉及到 ajax 请求,需要添加 json 支持 )
带参
1.数据绑定
2.使用 Httpservletrequest 对象获取前台传递过来的数据(涉及到表单的时候可以考虑使用)
不带参方法不带参数即可
jsp 代码:
<input type="submit" id="sub" value="提交" οnclick="ajaxClick();">
js 代码:
function ajaxClick(){
$.ajax( {
url : "<%=request.getContextPath()%>/test/ajaxTest",
data : {'name':"wsy"},
dataType: "json",
success : function(data) {
alert(data.name);
}
});
};
ctrl 代码:
@RequestMapping("/ajaxTest")
public @ResponseBody Map<String, Object> ajaxTest(@RequestParam("name")String name){
Map<String, Object> modelMap = new HashMap<String, Object>();
modelMap.put("name", "我的菜!");
return modelMap;
}
数据返回
1.使用 ModelMap 对象,作为方法参数传入
2.使用 Map 对象
5.文件上传
jsp 代码:
<form action="<%=request.getContextPath()%>/test/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="submit" value="submit">
</form>
ctrl 的写法 (直接上代码):
MultipartHttpServletRequest mreq = (MultipartHttpServletRequest)req;
MultipartFile file = mreq.getFile("file");
String fileName = file.getOriginalFilename();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
FileOutputStream fos = new FileOutputStream(req.getServletContext().getRealPath("/")+
"upload/"+sdf.format(new Date())+fileName.substring(fileName.lastIndexOf('.')));
fos.write(file.getBytes());
fos.flush();
fos.close();
return "hello";