赞
踩
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--配置中央控制器--> <!--只要服务器启动,这个servlet就调用init方法读取spring的配置文件,并且接收所有请求--> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--init方法可以通过ServletConfig来读取这个初始化参数--> <init-param> <param-name>contextConfigLocation</param-name> <!--读取spring配置文件--> <param-value>classpath:applicationContext-mvc.xml</param-value> </init-param> <!--服务器启动就装载这个servlet,直接创建servlet实例,调用init方法--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <!--所有的请求都交给这servlet处理--> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
<%-- Date: 2024/2/23 Time: 20:44 User: 孙显圣 Version:1.0 --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action=""> username:<input name="username" type="text"> password:<input name="password" type="password"> <input type="submit" value="登录"> </form> </body> </html>
package com.sun.web; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * @author 孙显圣 * @version 1.0 */ //表示这是一个控制器 @Controller public class UserServlet { //这个方法的网址是上下文路径 + login @RequestMapping(value = "/login") public String login() { System.out.println("login ok..."); //视图解析器配置的前缀是/WEB-INF/pages/ 后缀是.jsp所以这个资源的路径就是/WEB-INF/pages/login_ok.jsp return "login_ok"; //返回的这个值会交给视图解析器,指定要跳转的页面 } }
<%-- Date: 2024/2/23 Time: 20:54 User: 孙显圣 Version:1.0 --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>ok!</h1> </body> </html>
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--容器扫描--> <context:component-scan base-package="com.sun.web"/> <!--配置视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!--配置前缀和后缀--> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
package com.sun.web; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * @author 孙显圣 * @version 1.0 */ @Controller @RequestMapping(value = "/user") public class UserHandler { /** * 1.requestMethod支持四个常用选项,get,post,delete,put * 2.如果不写method参数则默认支持get和post * 3.目前这个buy方法的访问路径是上下文路径 + user/buy */ @RequestMapping(value = "/buy", method = RequestMethod.POST) public String buy() { System.out.println("buy方法被调用"); return "success"; } }
<%-- Date: 2024/2/24 Time: 12:40 User: 孙显圣 Version:1.0 --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>success</h1> </body> </html>
<%-- Date: 2024/2/24 Time: 12:43 User: 孙显圣 Version:1.0 --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="user/buy" method="post"> <input type="submit" value="提交"> </form> </body> </html>
/**
* 会将参数bookid自动填充到方法中
* @param bookid
* @return
*/
@RequestMapping(value = "/find", method = RequestMethod.POST, params = "bookid")
public String search(String bookid) {
System.out.println("bookid=" + bookid);
return "success";
}
<%-- Date: 2024/2/24 Time: 12:43 User: 孙显圣 Version:1.0 --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="user/find" method="post"> 输入bookid:<input name="bookid" type="text"> <input type="submit" value="提交"> </form> </body> </html>
/**
* 要求可以匹配 /user/message/aa, /user/message/aa/bb/cc
* @return
*/
@RequestMapping(value = "/message/aa/**")
public String im() {
System.out.println("匹配成功");
return "success";
}
/**
* 可以编写路径变量,直接在路径中接受变量,结合@PathVariable来进行参数传递
*
* @return
*/
@RequestMapping(value = "/reg/{username}/{password}")
public String register(@PathVariable("username") String name, @PathVariable("password") String password) {
System.out.println("username = " + name + " password = " + password);
return "success"; //请求转发
}
/**
* 两个重复的url
*/
@RequestMapping("/l1")
public String h1() {
return "success";
}
@RequestMapping("/l1")
public String h2() {
return "success";
}
/**
* 这样表示的就是get类型的请求,请求地址为“/buy1”
*/
@GetMapping("/buy1")
public String buy_() {
return "success";
}
//提交的信息如果包含这个参数则会自动填充,否则会传入一个空值
@GetMapping("/hello")
public String hello(String email) {
System.out.println(email);
return "success";
}
//课后练习
@PostMapping("/computer")
public String computer(String brand, String price, String num) {
System.out.println("brand=" + brand + " price=" + price + " num=" + num);
return "success";
}
<%-- Date: 2024/2/24 Time: 15:21 User: 孙显圣 Version:1.0 --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="user/computer" method="post"> 品牌:<input name="brand" type="text"><br> 价格:<input name="price" type="text"><br> 数量:<input name="num" type="text"><br> <input type="submit" value="提交"> </form> </body> </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。