Spring MVC文件配置以及参数传递示例详解
作者:朱怀昌 时间:2023-06-03 23:29:48
web.xml文件配置
创建好一个SpringMVC项目后,需要在需要在WB-INF文件夹下配置web.xml文件
<?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_3_1.xsd"
version="3.1">
<display-name>SpringMVCdemo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--加载springMVC的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:springMVC.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<!--中央核心控制器-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<!--请求-->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!--过滤器,编码格式-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
springMVC.xml文件配置
在src文件夹下创建springMVC.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.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">
<!--自动扫描上下文包-->
<context:component-scan base-package="cn.zhc.*"></context:component-scan>
<!--自动开启MVC模式注解-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--将请求映射到标注@RequestMapping注解的控制器和处理方法上-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀后缀-->
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
第一个SpringMVC实例
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
哈哈哈哈哈
</body>
</html>
测试类:
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 "index";
}
@RequestMapping("/hello.do")
public String hello(Map<String,Object> map){
map.put("name","lisa");
return "index";
}
总结
来源:https://blog.csdn.net/qq_43928469/article/details/115048026
标签:springmvc,文件,配置
0
投稿
猜你喜欢
深入分析C# Task
2022-10-24 12:39:00
C#生成随机数实例
2023-09-17 23:28:28
教你使用java实现去除各种空格
2022-09-21 21:27:07
C#中的问号(?号)用法小结
2023-12-26 08:03:25
Spring中的REST分页的实现代码
2023-03-16 01:06:46
Java中的final关键字使用方式
2023-01-16 15:04:25
浅谈JMeter engine启动原理
2022-02-22 03:06:47
C#中static的详细用法实例
2021-05-27 21:37:00
Android实现登录注册功能
2023-07-31 09:35:44
Java中Maven项目导出jar包配置的示例代码
2023-01-26 20:09:32
java的接口解耦方式
2023-04-17 00:36:45
Java中Collection、List、Set、Map之间的关系总结
2022-11-18 21:41:42
Java类加载机制实现流程及原理详解
2022-05-26 02:53:24
通过Html网页调用本地安卓(android)app程序代码
2022-05-10 10:14:34
详解SpringBoot下文件上传与下载的实现
2021-08-15 15:44:13
hadoop是什么语言
2021-06-18 07:02:38
Android中使用Toast.cancel()方法优化toast内容显示的解决方法
2021-12-14 05:17:03
Flutter Widget 之package mason实现详解
2021-07-08 23:43:08
C#编程和Visual Studio使用技巧(上)
2021-10-01 00:54:27
java+opencv实现人脸识别功能
2023-02-20 21:52:58