关于SpringMVC的XML配置步骤

1).配置前端控制

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <welcome-file-list>
        <welcome-file>/login.jsp</welcome-file>
    </welcome-file-list>
<!--    配置前端控制器-->
    <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:spring-mvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
​
​
</web-app>

2).根据前端控制器里面的init- param里面的classpath配置spring-mvc.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: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 
         https://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/mvc 
          https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.qiao"/>
​
<!--    视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>
<!--json结果类型转换为自动结果类型转换-->
<mvc:annotation-driven/>
​
<!--    全部静态资源放行-->
<mvc:default-servlet-handler/>
​
<!--    局部资源放行 mapping指定文件为js及其子包,location去对应的路径去寻找-->
<mvc:resources mapping="/js/**" location="/js/" />
</beans>
  • 在里面配置视图解析器

  • 静态资源的放行

  • json结果类型的自动结果类型转换,一句代码搞定,不需要记住那么复杂的xml配置

3).写controller层代码

package com.qiao.controller;
​
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
​
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
​
@Controller
public class Param {
    @RequestMapping("/param1")
    public void method1(HttpServletRequest req, HttpServletResponse resp, HttpSession session) throws Exception{
        req.setAttribute("data1","request域存的内容");
        session.setAttribute("data2","session域存的内容");
        req.getRequestDispatcher("/WEB-INF/jsp/result.jsp").forward(req,resp);
    }
    @RequestMapping("/param2")
    public String method2(Model model , ModelMap modelMap){
        model.addAttribute("data1","data1内容");
        modelMap.addAttribute("data2","data2内容");
        return "result2";
    }
​
    @RequestMapping(value = "/param3",method = RequestMethod.GET,produces = {"text/json;charset=utf-8"})
    @ResponseBody
    public String method3(@RequestParam(value = "id",defaultValue ="66") Integer UserId, String name){
        return UserId+":"+name;
    }
​
}
​

关于mvc:view-controller的用法

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringMVC是一种基于Java的Web框架,它可以帮助我们快速地开发Web应用程序。下面是SpringMVC配置步骤: 1. 配置DispatcherServlet 在web.xml文件中配置DispatcherServlet,它是SpringMVC的核心组件,负责处理所有的请求和响应。以下是一个示例配置: ```xml <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> ``` 2. 配置SpringMVC上下文 在上面的配置中,我们指定了SpringMVC上下文的位置为/WEB-INF/springmvc-servlet.xml。在这个文件中,我们可以配置SpringMVC的各种组件,例如控制器、视图解析器、拦截器等等。以下是一个示例配置: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!-- 配置控制器 --> <bean class="com.example.MyController"/> <!-- 配置视图解析器 --> <bean

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值