SpringMVC之Controller简单使用
2024-09-07 09:18:14
//环境 spring-4.3.18/JDK1.8/开发工具/IntelliJ IDEA 2018.2.5 x64
//工程结构图
//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_4_0.xsd"
version="4.0">
<!--配置Spring IoC配置文件路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<!--初始化Spring IoC容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--Spring MVC会根据servlet-name配置,找到/WEB-INF/dispatcher-servlet.xml作为配置文件载入web工程-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置Spring IoC配置文件路径-->
<!--<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--Controller-Servlet拦截配置-->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
//applicationContext.xml 该文件可省略,同时将
<!--配置Spring IoC配置文件路径-->
7 <context-param>
8 <param-name>contextConfigLocation</param-name>
9 <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/dispatcher-servlet.xml</param-value>
10 </context-param> 改成
<!--配置Spring IoC配置文件路径-->
7 <context-param>
8 <param-name>contextConfigLocation</param-name>
9 <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
10 </context-param>
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> </beans>
//dispatcher-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--使用注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>-->
<!--扫描装载的包-->
<context:component-scan base-package="com.jhc.controller"></context:component-scan>
<!--定义视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean> </beans>
//index.jsp
<%--
Created by IntelliJ IDEA.
User: JHC
Date: 2018/11/30
Time: 17:31
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Hello Word</title>
</head>
<body>
Say:${hello}<br>
</body>
</html>
//HelloController.java
package com.jhc.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
public class HelloController { @RequestMapping(value = "/hello")
public ModelAndView sayHello(){
System.out.println("Hello Word");
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("hello","Hello Word");
modelAndView.setViewName("/index.jsp");
return modelAndView;
}
}
最新文章
- python unicode字节串转成中文问题
- Collection集合List、Set
- UBUNTU12.4 安装磊科无线网卡驱动
- 欧拉工程第70题:Totient permutation
- [转]oracle的ANYDATA数据类型
- 十七、C# 反射、特性和动态编程
- 百度搜索URL参数 搜索关键字
- JavaEE Tutorials (20) - 企业应用安全入门
- 总结下js中匿名函数的写法~好几天没写博客了。。。
- javascript编辑器预览模式解密
- UVA 1364 - Knights of the Round Table (获得双连接组件 + 二部图推理染色)
- Robotframe work学习之初(二)
- 【MongoDB】使用MongoVUE看不到插入的数据
- HDU - 4324 Triangle LOVE(拓扑排序)
- php获取目录下所有文件路径(递归)
- Centos7手动编译安装Python3
- 删除oracle实例
- nginx+lua_module安装
- C++中的explicit关键字(转)
- 【音乐App】—— Vue-music 项目学习笔记:播放器内置组件开发(二)