(三)控制器Controller中的方法通过return 返回特定JSP页面

效果:



重要部分:



-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1.目录树:


-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2.需要导入的jar包


为稳阵起见,建议全部spring的jar包都导入

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

3.web.xml 文件

用于初始化控制器Controller

<?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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>pro4</display-name>
	<!-- 定义springMVC的前端控制器(相当于一个servlet) -->
	<servlet>
		<!-- springMVC 控制器配置 -->
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>	   <!-- 为spring核心文件起一个别名,可任意起 -->
			<param-value>/WEB-INF/spring-mvc.xml</param-value> <!-- 指定spring的核心配置文件的位置 -->
		</init-param>
		<load-on-startup>1</load-on-startup>				   <!-- 设置启动服务器后马上进行配置 -->
	</servlet>

	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>           <!-- 控制器的别名,要与上面的<servlet-name>一样-->
		<url-pattern>/</url-pattern>					
	</servlet-mapping>
</web-app>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

4.spring核心配置文件(spring-mvc.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.xsd
	   	http://www.springframework.org/schema/mvc
	   	http://www.springframework.org/schema/mvc/spring-mvc.xsd
	   	http://www.springframework.org/schema/context
	   	http://www.springframework.org/schema/context/spring-context.xsd">
	   	<!-- 上面是固定约束,下面是进行springmvc配置 -->
	   	<!-- 下三行分别是,开启注解扫描,配置映射器,配置适配器,配置视图解析器 -->
	   	<context:component-scan base-package="controller"></context:component-scan>
		<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
		<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix">
				<value>/WEB-INF/</value>	<!-- 表示要调用的文件前缀,即表示要调用的文件在/WEB-INF/路径下 -->
			</property>
			<property name="suffix">
				<value>.jsp</value>			<!-- 表示要调用的文件后缀,即表示是jsp文件 -->
			</property>
		</bean>
</beans>

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

5.控制器文件(UserController.java)

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/*MVC模式中的C,即控制器*/
@Controller
public class UserController {
	@RequestMapping(value="/abc")		//映射的路径为http://localhost:8080/项目名/abc
	public String abc_func(){
		return "abc";					//返回abc字符串给spring-mvc.xml,组成/WEB-INF/abc.jsp,然后调用该jsp文件
	}
	
	@RequestMapping(value="/efg")		//映射的路径为http://localhost:8080/项目名/efg
	public String efg_func(){
		return "efg";					////返回efg字符串给spring-mvc.xml,组成/WEB-INF/efg.jsp,然后调用该jsp文件
	}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
6.JSP文件(即abc.jsp 和 efg.jsp)

abc.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>
<body>
	<h1>abc.jsp</h1>
</body>
</html>


efg.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>efg.jsp</h1>
</body>
</html>

最后,在浏览器输入:http://localhost:8080/pro5/abc 就会调用/WEB-INF/下的abc.jsp 文件

这是根据注解@RequestMapping 实现的



猜你喜欢

转载自blog.csdn.net/u014453898/article/details/79559822