Spring Mvc的xml配置和测试
2017-10-25 19:04
225 查看
1.配置文件(web.mxl和***-servlet.xml,这里是springmvc-servlet.xml)
web.xml配置
springmvc-servlet.xml配置
2.控制层Controller
web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>SpringMvc1</display-name> <servlet> <servlet-name>springmvc</servlet-name> <!-- servlet分发器 --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 进入servletmvc框架的匹配规则 --> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> </web-app>
springmvc-servlet.xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 启动spring包扫描 --> <context:component-scan base-package="com.gbl.controller"/> <!-- 处理静态资源 --> <mvc:annotation-driven/> <mvc:default-servlet-handler/> <!-- 配置视图解析器开始 前面说的前缀和后缀--> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 配置视图解析器结束 --> <!-- 配置文件上传 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- one of the properties available; the maximum file size in bytes --> <property name="maxUploadSize" value="100000"/> </bean> <!-- 找到控制层的类 --> <bean id="user" class="com.gbl.controller.UserController"></bean> </beans>
2.控制层Controller
//通过注解找到这个类文件 @Reques 4000 tMapping(value = "/user") public class UserController{ @RequestMapping(value = "/hello") public String hello(){ return "hello"; //通过springmvc-servlet.xml配置视图解析器,给字符串 //加前缀和后缀,然后访问这个jsp文件 } @RequestMapping(value="login") //参数名必须与前台的name属性一样 //直接封装User对象 //向页面传值 用ModelMap或者用request、session public String login(User user,ModelMap map){ System.out.println("进入login Controller"); //使用ModelMap传值,页面${uname},${password}获取 map.put("uname", user.getUname()); map.put("password", user.getPassword()); //redirect 重定向;forward 转向 return "redirect:loginSuccess.html"; } @RequestMapping(value="loginSuccess.html") //假装是html public String loginSuccess(){ System.out.println("进入loginSucess.html Controller"); return "loginSuccess"; } public String upload(@RequestParam("upload")MultipartFile mf, HttpServletRuquest request){ //获取项目在tomcat里的全路径 String path = request,getServletContext().getRealPath("/fileUpload"); //request.getContextPath(); 从项目名开始的路径 //request.getServletContext().getResource("/"); //file:项目在tomcat里的全路径 //mf.getName(); 上传文件的前台name属性值 //mf.getOriginaFilename(); 文件名(包括后缀) mf.transferTo(new File(path+"/"+mf.getOriginalFilename())); //request.getSession().setAttribute("upload",img.getOriginalFilename()); 把文件地址放进session里 return "页面"; } }
相关文章推荐
- spring mvc +hibernate注解版测试增,删,改,查 简单xml配置应用
- Spring4 Spring MVC实战(三)——Spring MVC不通过xml配置访问HMTL和其他静态资源
- SSH框架整合时applicationContext.xml的配置,关于在main方法中测试注意的事项
- Spring MVC的web.xml配置详解
- Spring MVC静态资源处理(在applicationContex.xml文件中进行配置)
- Spring MVC的WebMvcConfigurerAdapter用法收集(零配置,无XML配置)
- 关于spring MVC web.xml配置文件的编写总结(持续更新)
- 2.6.2 XML配置:使用testNG进行并发多浏览器测试
- [置顶] Spring mvc通过xml配置文件方式实现简单HelloWorld
- maven管理Spring MVC项目pom.xml配置
- maven打包去掉测试的部分pom.xml配置
- spring Mvc 执行原理 及 xml注解配置说明 (六)
- spring mvc 静态资源配置 注意web.xml中DispatcherServlet配置
- Spring MVC 教程,快速入门,深入分析――springMVC-mvc.xml 配置文件片段讲解
- spring mvc的web.xml的配置
- 关于spring MVC web.xml配置文件的编写总结(持续更新)
- Spring MVC Junit测试实例 附:读取WEB-INF下配置文件的方法
- spring mvc 中web.xml配置信息解释
- 基于spring3.0的采用XML配置的spring MVC项目
- Android中的测试类配置AndroidManifest.xml