1在ider中创建mven工程
2.(1)搭建MVC框架创建完Maven工程后点击以Web开头的选项后ok创建,此时我们的工程就出现了一个Web
(2)
在java文件夹下创建com.bjsxt.controller包,
在包下创建Mycontrooler.java文件
在resources文件夹下创建springmvc.xml文件,
在web文件夹下创建adim.jsp文件
web.xml文件在其中添加如下代码
<!-- springmvc的前端控制器 -->
<servlet>
<servlet-name>springmvc</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>springmvc</servlet-name>
<url-pattern>/</url-pattern><!--拦截除jsp请求以外的所有请求-->
</servlet-mapping>
在Mycontroller中编写如下代码:Mycontroller是控制类,负责处理servlet的请求。
然后在springmvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!--自动扫描目录下所有的类文件 -->
<context:component-scan base-package="com.bjsxt.controller" />
<!--默认注解映射的支持 -->
<mvc:annotation-driven />
</beans>
改代码的功能是将controller包中的文件配置到springmvc的容器当中。
之后在Mycontroller类添加如下代码:
@RequestMapping("demo")
@ResponseBody
public String demo(HttpServletRequest request) throws ServletException, IOException {
String name=request.getParameter("name");
String password=request.getParameter("password");
Map map=new HashMap();
map.put("name",name);
map.put("password",password);
request.setAttribute("map",map);
System.out.println(map);
return null;
}
@RequestMapping("demo2")
@ResponseBody
public String demo2() {
System.out.println("我是单元方法demo2");
return "demo2";
}
@RequestMapping("demo3")
@ResponseBody
public String demo3(String name){
return "demo3_name="+name;
}
在adim.jsp中编写如下代码:
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2021/9/19
Time: 16:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table cellpadding="10">
<form method="post" action="demo">
<tr>
<td>姓名:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="password"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"/></td>
</tr>
</form>
</table>
</body>
</html>