springMVC基本环境配置




1.新建一个web项目

2.导入spring需要的jar包,准备好spring-mvc.xml文件(可以从官网或其它地方下载)

3.配置前端控制器web.xml

<?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>zhouSpringMVC2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 
    <!-- 核心功能处理器 -->
  <servlet>
      <servlet-name>dispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <!-- 启动的时候需要读取spring的配置文件 -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <!-- 随着web容器tomcat的启动,实例化这个servlet >=0 数字越小,启动越早-->
    <load-on-startup>1</load-on-startup>
  </servlet>
 
  <servlet-mapping>
      <servlet-name>dispatcherServlet</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>
 
</web-app>


4.配置springMVC的Controller(三种方法)

第一种方法(实现Controller接口)
1.新建一个MyController类实现Controller接口并重写handleRequest方法,源代码如下:

package com.zy.controller;

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

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

public class MyController implements Controller{
    
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        /*
         * ModelAndView对象,模型和视图对象,对象中既可以添加数据,也可以包含视图
         *
         */
        ModelAndView mav = new ModelAndView();
        
        //添加数据,类似于request.setAttribute(String,Object);
        mav.addObject("mess","用实现Controller接口的方式实现");
        //设置视图,你的请求去哪里
        mav.setViewName("/WEB-INF/jsp/success.jsp");
        return mav;
    }
    
}

2.配置spring-mvc.xml文件中的Controller
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!-- 实例化控制器 -->
        <bean name="/mycontroller" class="com.zy.controller.MyController"/>
        
       
 </beans>
 
 3.编写jsp文件测试页面跳转
 
 (index.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>test</title>
</head>
<body>
<a href="mycontroller">1号方案</a><br>

</body>
</html>

(succes.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>
${mess }
</body>
</html>


第二种方法(实现HttpRequestHandler接口)
1.新建一个MyController2类实现HttpRequestHandler接口并重写handleRequest方法,源代码如下:

package com.zy.controller;

import java.io.IOException;

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

import org.springframework.web.HttpRequestHandler;

public class MyController2 implements HttpRequestHandler{

    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        
        request.setAttribute("mess", "用实现HttpRequestHandler接口的方式");
        
        request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);;
        
    }
    
}

2.配置spring-mvc.xml文件中的Controller
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!-- 实例化控制器 -->
        <bean name="/mycontroller" class="com.zy.controller.MyController"/>
        
        <bean name="/mycontroller2" class="com.zy.controller.MyController2"/>

 </beans>
 
 3.编写jsp文件测试页面跳转

(index.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>test</title>
</head>
<body>
<a href="mycontroller">1号</a><br>
<a href="mycontroller2">2号</a><br>
</body>
</html>

(success.jsp页面与上同)


第三种方法(基于注解的控制器)
1.新建一个MyController3类源代码如下:
package com.zy.controller;

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

@Controller
public class MyController3 {
    @RequestMapping("/mycontroller3")
    public ModelAndView test1(){
        
        ModelAndView mav = new ModelAndView();
        mav.addObject("mess","1号用注解开发控制器");
        mav.setViewName("/WEB-INF/jsp/success.jsp");
        return mav;
    }
    @RequestMapping("/mycontroller4")
    public ModelAndView test2(){
        ModelAndView mav = new ModelAndView();
        mav.addObject("mess","2号用注解开发控制器");
        mav.setViewName("/WEB-INF/jsp/success.jsp");
        return mav;
    }
}

2.配置spring-mvc.xml文件中的Controller

<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!-- 实例化控制器 -->
        <bean name="/mycontroller" class="com.zy.controller.MyController"/>
        
        <bean name="/mycontroller2" class="com.zy.controller.MyController2"/>
        
         <!-- 扫描包 -->
         <context:component-scan base-package="com.zy" />
 </beans>
 
 3.编写jsp文件测试页面跳转

(index.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>test</title>
</head>
<body>
<a href="mycontroller">1号</a><br>
<a href="mycontroller2">2号</a><br>
<a href="mycontroller3">3号</a><br>
<a href="mycontroller4">4号</a><br>
</body>
</html>

(success.jsp文件如上)


猜你喜欢

转载自blog.csdn.net/qq_37812895/article/details/78542909