springMVC框架入门案例
控制器:
package cn.mepu.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * @User 艾康 * @create 2019-11-12 10:24 * 控制器 */ @Controller public class HelloController { //url请求时执行该方法 method:请求方式 params该配置表示必须传入username参数 headers请求头必须包含的请求头 @RequestMapping(path = "/hello",method = {RequestMethod.GET},params = {"username"},headers = {}) public String sayHello(){ System.out.println("入门案例"); return "success"; } }
index:
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/11/12 Time: 9:43 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>入门案例</title> </head> <body> <h3>入门案例</h3> <a href="hello">入门案例</a> </body> </html>
成功页面:
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/11/12 Time: 9:44 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>入门成功</title> </head> <body> <h3>入门成功</h3> </body> </html>
web.xml配置:
<!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>
springMVC的配置:
<?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="cn.mepu"></context:component-scan> <!--配置试图解析器--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 跳转文件路径--> <property name="prefix" value="\WEB-INF\pages\"></property> <!-- 跳转文件后缀--> <property name="suffix" value=".jsp"></property> </bean> <!--开启注解--> <mvc:annotation-driven></mvc:annotation-driven> </beans>
相关推荐
boneix 2020-10-21
JudeJoo 2020-08-21
meleto 2020-08-15
haidaoxianzi 2020-07-04
小鱿鱼 2020-06-26
咻pur慢 2020-06-15
haidaoxianzi 2020-06-14
qingjiuquan 2020-06-07
zhangdy0 2020-05-31
qingjiuquan 2020-05-30
HappyHeng 2020-05-16
whbing 2020-04-17
whbing 2020-04-11
小鱿鱼 2020-03-20
mendeliangyang 2020-02-20
方志朋 2020-02-14
牧场SZShepherd 2020-02-01