SpringMVC环境搭建&&入门案例浅谈

it2025-01-14  5

环境搭建

路径说明:关于 / 的问题(自己学习中困惑的,也是自己总结出来的,不全,欢迎补充!!)。 可以不加 / 的情况: 1、jsp页面

<a href="account/findAll">测试1</a><a href="account/findAll">测试2</a>

2、Controller页面

@RequestMapping("/findAll")@RequestMapping("save") 必须加 / 的情况: 重定向:responseresponse.sendRedirect(request.getContextPath()+"/account/findAll");

1. New Project 选择maven->勾选Create from archetype->选择maven-archetype-webapp 添加一组值,为了更快构建springMVC结构。 archtypeCatalog internal

2. 补全项目结构。java&&resources,并且添加为资源文件。

3. 配置pom.xml中的jar包依赖 注:红色框里为了版本锁定,注意自己的jdk版本

4. 配置前端控制器:web.xml 注:我途中是拦截所有

<web-app> <display-name>Archetype Created Web Application</display-name> <!-- 配置前端控制器--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--加载springmvc.xml--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!--启动服务器,创建该servlet--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置中文乱码的过滤器--> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

入门案例

1.index.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>入门程序</title> </head> <body> <h3>入门程序</h3> <a href="hello">点我</a> </body> </html>

2.web.xml被加载 拦截了所有页面,此时DIspatcherServlet对象创建。

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

2.springmvc.xml被加载 创建了视图解析器对象,定位到success.jsp.

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!--开启扫描注解--> <context:component-scan base-package="cn.itcast"/> <!-- 视图解析器对象--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> <!--开启SpringMVC框架注解的支持--> <mvc:annotation-driven/> </beans>

4.HelloController创建成对象 控制器类 ,调用sayHello方法

import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; //控制器类 @Controller public class HelloController { @RequestMapping(path = "/hello") public String sayHello(){ System.out.println("Hello springMVC!"); return "success"; } }

后台处理数据过程

SpringMVC框架基于组件方式执行流程:

最新回复(0)