编辑这个页面须要登录或更高权限!
在Spring MVC中,我们可以一次创建多个控制器。必须使用 @Controller 注释映射每个控制器类。在这里,我们看到了多个控制器的Spring MVC示例。步骤如下:
在Maven中加载spring jar文件或添加依赖项 创建控制器类 在web.xml文件中提供控制器的条目 在单独的XML文件中定义bean 创建其他视图组件 启动服务器并部署项目
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.1.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> </dependency>
让我们创建一个包含两个链接的简单JSP页面。
index.jsp
<html> <body> <a href="hello1">Spring MVC</a> || <a href="hello2">Spring Boot</a> </body> </html>
让我们创建两个控制器类,每个控制器类返回特定的视图页面。
HelloController1.java
package com.nhooo; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController1 { @RequestMapping("/hello1") public String display() { return "viewpage1"; } }
HelloController2.java
package com.nhooo; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController2 { @RequestMapping("/hello2") public String display() { return "viewpage2"; } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringMVC</display-name> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
spring-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:context="http://www.springframework.org/schema/context" 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/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"> <!-- Provide support for component scanning --> <context:component-scan base-package="com.nhooo" /> <!--Provide support for conversion, formatting and validation --> <mvc:annotation-driven/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
viewpage1.jsp
<html> <body> <p>Welcome to Spring MVC Tutorial</p> </body> </html>
viewpage1.jsp
<html> <body> <p>Welcome to Spring Boot Tutorial</p> </body> </html>
输出: