欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springMVC实现示例

程序员文章站 2022-03-18 19:18:03
...

springMVC实现示例

1、导入相应Jar包

2、在web.xml中配置Servlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- 核心控制器的配置DispatchServlet -->
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>
  		org.springframework.web.servlet.DispatcherServlet
  	</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc-servlet.xml</param-value>
  	</init-param>
  	<!-- 标记容器是否在web应用启动时就加载这个servlet(实例化并调用其init()方法),必须是一个整数,表示servlet被加载的先后顺序,
    	如果没有设置或为负数,则容器会当Servlet被请求时再加载,如果值为正整数或者为0时,表示在应用启动时就加在并初始化这Servlet,值越小,
    	Servlet的优先级越高,越先被加载,值相同时,容器根据自己选择顺序加载     -->
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<!--这里的servlet-name对应上面的servlet中的servlet-name,要相同 -->
  	<servlet-name>springmvc</servlet-name>
  	<!-- /代表springmvc将会拦截处理项目中所有url请求 -->
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

3、创建springmvc-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:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<!-- 配置处理器映射HandlerMapping -->
	<bean name="/index.html" class="cn.smbms.controller.IndexController"></bean>
		
	<!-- 视图解析器/WEB-INF/jsp/index.jsp -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 给返回的值加上前缀和后缀,将返回的值变成/WEB-IN/jsp/**.jsp -->
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>

4、配置Controller

package cn.smbms.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class IndexController extends AbstractController {

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("hahaha");
		//这里返回的值,视图解析器会给它加上前缀和后缀,变成网址:http://localhost:8080/SMBMS_MVC/index.jsp
		return new ModelAndView("index");
	}

}

5、创建View

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    jsp,jsp
  </body>
</html>

springMVC实现示例

以上就可以实现一个简单的springmvc,还可以通过注解的方式,把一个URL映射到Controller上,需要修改springmvc-servlet.xml

6、HandlerMapping(处理映射器)

<?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:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<!-- 配置处理器映射HandlerMapping -->
	<!-- <bean name="/index.html" class="cn.smbms.controller.IndexController"></bean> -->
	
	<!-- 自动注册bean -->
	<mvc:annotation-driven></mvc:annotation-driven>
	<!-- 对包进行扫描,将bean注入容器 -->
	<context:component-scan base-package="cn.smbms.controller"></context:component-scan>
	
	<!-- 视图解析器/WEB-INF/jsp/index.jsp -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>

7、Controller修改

以下代码都可以实现与上面代码的相同效果,还可以通过@RequestMapping("/index")来实现多个方法。

package cn.smbms.controller;

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

@Controller
public class IndexController{

	@RequestMapping("/index")
	public String index(){
		System.out.println("springMVC");
		return "index";
	}
	
}
package cn.smbms.controller;

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

@Controller
public class IndexController{

	@RequestMapping("/index")
	public ModelAndView index(){
		System.out.println("springMVC");
		return new ModelAndView("index");
	}
	
}

springMVC实现示例

相关标签: springmvc