springMVC

    科技2022-07-10  148

    SpringMVC环境搭建

    补全目录-添加 java和resources文件夹在pom.xml中导入jar包 <!-- 版本锁定 --> <properties> <spring.version>5.0.2.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> </dependencies> 在web.xml中配置前端控制器servlet <!-- SpringMVC的核心控制器 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置Servlet的初始化参数,读取springmvc的配置文件,创建spring容器 --> <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> 在resources下创建springMVC.xml,添加约束 <?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"> <!-- 配置spring创建容器时要扫描的包 --> <context:component-scan base-package="com.itheima"></context:component-scan> <!-- 配置视图解析器 --> <bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 配置spring开启注解mvc的支持 --> <mvc:annotation-driven></mvc:annotation-driven> </beans>

    入门程序编写

    更改springMVC.xml中的约束,开启注解扫描index.jsp中添加链接,点击链接跳转到成功界面写一个控制器servlet,类加注解@Controller,写一个返回字符串的方法,返回值为success.方法加注解@RequestMapping(path = "/hello"),hello为路径在WEB-INF中新建文件夹pages,pages中新建 success.jsp在springMVc.xml中写视图解析器<!--视图解析器--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <!--成功界面所在位置--> <property name="suffix" value=".jsp"/> <!--成功界面的类型--> </bean>

    7.在web.xml中加载springMVC.xml文件还有服务器启动时执行

    <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--加载springMVC文件--> <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>

    RequestMapping注解

    RequestMapping注解的作用 类上加@RequestMapping(paht = "/user"),方法上加@RequestMapping(path = "/test")。 a标签上的路径应该是"user/test"。RequestMapping注解的属性 1.path和value属性:@RequestMapping(path = "/test")和@RequestMapping(value = "/test")作用是相同的,只有value属性时value也可以省略。@RequestMapping("/test") 2.method属性:@RequestMapping(path = "/hello",method = RequestMethod.GET),method代表方法执行的方式。 3.params:用于指定限制请求参数的条件。@RequestMapping(path = "/hello",params = {"username"}),请求参数中必须有username,没有则不执行。 4.headers 发送的请求中必须包含的请求头@RequestMapping(path = "/hello",params = {"username"},headers = {"Accept"})。
    Processed: 0.031, SQL: 8