[jbdj]SpringMVC框架(1)快速入门

时间:2023-03-08 16:56:47
[jbdj]SpringMVC框架(1)快速入门

1)springmvc快速入门(传统版)

步一:创建springmvc_demo一个web应用

步二:导入springioc,springweb , springmvc相关的jar包

步三:在/WEB-INF/下创建web.xml文件

Web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<!-- 注册springmvc框架核心控制器 -->

<servlet>

<servlet-name>DispatcherServlet</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>DispatcherServlet</servlet-name>

<url-pattern>*.action</url-pattern>

</servlet-mapping>

</web-app>

步四:创建Helloaction.java控制器类

Helloaction.java

package action;

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 helloaction implements Controller {  //实现方法

   /**

    * 

    * 业务方法

    * 实现方法

    **/

@Override

public ModelAndView handleRequest(HttpServletRequest request,

HttpServletResponse response) throws Exception {

//创建ModelAndView对象

ModelAndView  modelAndView = new ModelAndView();

//对其进行赋值

modelAndView.addObject("message", "这是我的第一个springmvc应用程序!");

modelAndView.setViewName("/jsp/success.jsp");

return modelAndView;

} 

}

步五:在/WebRoot/下创建jsp/success.jsp

success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>

  <head>

    <title>My JSP 'success.jsp' starting page</title>  

  </head>

  <body>

    success.jsp<br/>

${message}

  </body>

</html>
 

步六:在/WEB-INF/创建DispatcherServlet-servlet.xml配置文件,xml头部信息与spring.xml相同

注意:该配置文件的命名规则:web.xml文件中配置的<servlet-name>的值-servlet.xml'

DispatcherServlet-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:aop="http://www.springframework.org/schema/aop"

      xmlns:tx="http://www.springframework.org/schema/tx"

  xmlns:mvc="http://www.springframework.org/schema/mvc"

      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/aop 

  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   

  http://www.springframework.org/schema/tx

      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    

      http://www.springframework.org/schema/mvc

      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- 控制器(程序员) -->

    <bean name="/hello.action" class="action.helloaction"></bean>      

    <!-- 映射器(框架) -->  

    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>           

    <!-- 适配器(框架) -->  

    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>            

    <!-- 视图解析器(框架) -->  

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>             

</beans>
 

步七:部署web应用到tomcat中,通过浏览器访问如下URL:

http://127.0.0.1:8080/stringmvc_demo/hello.action