Spring MVC文件配置以及参数传递示例详解

网友投稿 349 2022-10-30


Spring MVC文件配置以及参数传递示例详解

web.xml文件配置

创建好一个SpringMVC项目后,需要在需要在WB-INF文件夹下配置web.xml文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

version="3.1">

SpringMVCdemo

index.jsp

contextConfigLocation

classpath*:springMVC.xml

org.springframework.web.conthttp://ext.ContextLoaderListener

dispatcher

org.springframework.web.servlet.DispatcherServlet

1

dispatcher

*.do

characterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

characterEncodingFilter

/*

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

version="3.1">

SpringMVCdemo

index.jsp

contextConfigLocation

classpath*:springMVC.xml

org.springframework.web.conthttp://ext.ContextLoaderListener

dispatcher

org.springframework.web.servlet.DispatcherServlet

1

dispatcher

*.do

characterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

characterEncodingFilter

/*

springMVC.xml文件配置

在src文件夹下创建springMVC.xml文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc.xsd">

第一个SpringMVC实例

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

哈哈哈哈哈

测试类:

package cn.zhc.test;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

@Controller

public class Test {

@RequestMapping("/hello.do")

public String hello(){

System.out.println("hhhhhhhhhhhh");

return "index";

}

}

在项目运行后,在前端页面路径后输入/hello.do,控制台会输出hhhhhhhhhhhh

参数传递

view到controller 四种方式

@RequestMapping("/hello.do")

public String hello(String name){

//路径后加?name= 不加会传null

System.out.println(name);

return "index";

}

//Controller方法方法中参数前加@RequestParam进行直接入参

@RequestMapping("/hello.do")

public String hello(@RequestParam String name){

//不传参会请求错误400

System.out.println(name);

return "index";

}

@RequestMapping("/hello.do")

public String hello(@RequestParam(value = "name" ,required = false) String name){

//required是否需要传参

System.out.println(name);

return "index";

}

@RequestMapping(value = "/hello.do",method = RequestMethod.GET,params = "name")

public String hello(String name){

//不传参会请求错误400

System.out.println(name);

return "index";

}

controller到view 三种方式

@RequestMapping("/hello.do")

public ModelAndView hello(){

ModelAndView mv = new ModelAndView();

mv.addObject("name","zhu");//添加模型数据

mv.setViewName("index");//设置视图名称

return mv;

}

@RequestMapping("/hello.do")

public String hello(Model model){

model.addAttribute("name","huai");

model.addAttribute("chang");

//在model中若不指定key,则使用默认对象的类型作为key

return "indeEZlxzx";

}

@RequestMapping("/hello.do")

public String hello(Map map){

map.put("name","lisa");

return "index";

}

总结


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:F5负载均衡设备升级
下一篇:路由器交换机命令总结
相关文章

 发表评论

暂时没有评论,来抢沙发吧~