Spring Mvc的xml配置和测试

1.配置文件(web.mxl和***-servlet.xml,这里是springmvc-servlet.xml)
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

//通过注解找到这个类文件
    @RequestMapping(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 "页面";

        }

    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值