项目结构:
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">
<!-- 配置 springmvc 核心控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置初始化参数:到 src 目录下加载 springmvc.xml 配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 开启注解扫描:扫描指定包下的所有注解;需要引入 context 名称空间 -->
<context:component-scan base-package="com.springmvc.demo"/>
</beans>
HelloAction.java:
package com.springmvc.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 控制器:单例
* @Controller 注解:表示当前类是一个控制器
*/
@Controller
public class HelloAction {
/**
* 业务方法:
* 可带参数,也可不带参数;(struts2 不可带参数)
* @RequestMapping 注解:表示请求的映射,所有 /hello.action 的请求,都会直接进入该方法
*/
@RequestMapping(value = "/hello.action")
public String hello(Model model) throws Exception{
// 封装数据
model.addAttribute("message", "你好");
// 直接返回视图的真实路径
return "/index.jsp";
}
/**
* 业务方法:
* @RequestMapping 注解:表示请求的映射,所有 /bye.action 的请求,都会直接进入该方法
*/
@RequestMapping(value = "/bye.action")
public String bye(Model model) throws Exception{
// 封装数据
model.addAttribute("message", "再见");
// 直接返回视图的真实路径
return "/index.jsp";
}
}
控制器中的业务方法不仅可以返回 视图的真实路径,也可以返回视图路径的逻辑名:
注意:返回视图路径的逻辑名时 必须配置视图解析器;
HelloAction.java:
package com.springmvc.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 控制器:单例
* @Controller 注解:表示当前类是一个控制器
*/
@Controller
public class HelloAction {
/**
* 业务方法:
* 可带参数,也可不带参数;(struts2 不可带参数)
* @RequestMapping 注解:表示请求的映射,所有 /hello.action 的请求,都会直接进入该方法
*/
@RequestMapping(value = "/hello.action")
public String hello(Model model) throws Exception{
// 封装数据
model.addAttribute("message", "你好");
// 返回视图路径的逻辑名
return "index";
}
}
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 开启注解扫描:扫描指定包下的所有注解;需要引入 context 名称空间 -->
<context:component-scan base-package="com.springmvc.demo"/>
<!-- 配置视图解析器:如果控制器中返回视图路径的逻辑名,则必须配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/> <!-- 配置视图路径的前缀 -->
<property name="suffix" value=".jsp"/> <!-- 配置视图路径的后缀 -->
</bean>
</beans>