Spring MVC文件配置以及参数传递示例详解
作者:朱怀昌 发布时间:2023-06-03 23:29:48
标签:springmvc,文件,配置
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


猜你喜欢
- 第一篇讨论了面向对象编程和它的特点,关于Java和它的功能的常见问题,Java的集合类,垃圾收集器,本章主要讨论异常处理,Java小应用程序
- 说明这里只以 servlet 为例,没有涉及到框架,但其实路径的基本原理和框架的关系不大,所以学了框架的同学如果对路径有疑惑的也可以阅读此文
- 实现引导小圆点的方法其实很简单,可直接在布局上放置与引导页面等量的ImageView,然后在切换页面的时候更改图片资源就好了。这里顺便提一下
- 基本用法不说了,网上例子很多,这里主要介绍下比较特殊情况下使用的方法。1. 分组有的时候,我们对一个实体类需要有多中验证方式,在不同的情况下
- 在Activity类的子类中直接复写下面三个方法://复写onCreateOptionsMenu()方法,弹出菜单栏
- 在LINUX上部署带有JAR包的JAVA项目首先eclipse上要装上一个小插件,叫做Fat Jar点击Fat Jar红框里选上主类点击Ne
- 本文实例讲述了C#画笔Pen绘制光滑模式曲线的方法。分享给大家供大家参考。具体实现方法如下:using System;using Syste
- 本文实例为大家分享了Android Studio实现简易计算器App的具体代码,供大家参考,具体内容如下效果演示布局文件<?xml v
- 一. 彩信发送: 彩信比短信麻烦很多。从sendMmsWorker函数的参数就可以看出来:(conv, mmsUri, pers
- 由于公司项目的需求,需要绘制一条竖直的间断线作为分割线。这个可坑了爹了,以前只搞过水平的间断线,只要通过shape也可以简单的画出来,但是千
- 本文实例为大家分享了Struts2+uploadify多文件上传的具体代码,供大家参考,具体内容如下首先我这里使用的是 Jque
- 本文实例为大家分享了Android Studio实现补间动画的具体代码,供大家参考,具体内容如下补间动画是给出初始位置和结束位置,中间由系统
- 要求:1.通过手指移动来拖动图片 2.控制图片不能超出屏幕显示区域技术点:1.MotionEvent处理2.对View进行动态定位
- NDK部分1、下载ndk这里就一笔带过了。2、解压ndk不要解压,文件权限会出错。执行之,会自动解压,然后mv到想放的地方。我放到了”/us
- 简介常见的4种使用线程的方法:1实现 Runnable 接口;2实现 Callable 接口;3继承 Thread 类。4匿名内部类的写法。
- 本文实例为大家分享了android自定义控件实现简易时间轴的具体代码,供大家参考,具体内容如下之前项目需要写一个消费记录,类似于时间轴似的控
- 设计模式要进行共性与可变性的分析,对共性进行抽象,同时对可变性进行封装,没有完美的设计模式,作为一名开发者要懂得取舍,触类旁通,开发出高内聚
- 在winform程序中给form添加了keyup事件,但是程序却不响应键盘事件,解决办法是重写Form基类的ProcessCmdKey(re
- 归纳一些网上取JAVA路径的方法: 注明:如果从ANT启动程序,this.getClass().getResource("&quo
- 一、工具类代码public class TaskHelper {#region 多线程操作 &nbs