zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Spring MVC配置

2023-09-11 14:16:15 时间

1,添加相关jar包

2,配置web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 3 
 4     <!-- 过滤器,将浏览器的POST请求转化为PUT/DELETE -->
 5     <filter>
 6         <filter-name>hf</filter-name>
 7         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
 8     </filter>
 9     <filter-mapping>
10         <filter-name>hf</filter-name>
11         <url-pattern>/*</url-pattern>
12     </filter-mapping>
13     
14     <!-- 配置springDispatcherServlet -->
15     <servlet>
16         <servlet-name>springmvc</servlet-name>
17         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
18         <!-- 配置文件的路径和名称;也可以不配置,则默认名称为:上面servlet-name值后加"-servlet.xml"默认位置:WEB-INF文件夹下 -->
19         <!-- 
20         <init-param>
21             <param-name>contextConfigLocation</param-name>
22             <param-value>classpath:springmvc.xml</param-value>
23         </init-param>
24          -->
25         <!-- 启动时自动加载 -->
26         <load-on-startup>1</load-on-startup>
27     </servlet>
28 
29     <!-- 配置请求路径 -->
30     <servlet-mapping>
31         <servlet-name>springmvc</servlet-name>
32         <url-pattern>/</url-pattern>
33     </servlet-mapping>
34 
35 </web-app>

3,配置springmvc-servlet.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
 8         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
 9 
10 <!-- 扫描器 -->
11 <context:component-scan base-package="com.hanqi"></context:component-scan>
12 
13 <!-- 视图解析器 -->
14 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
15     <!-- 前缀 -->
16     <property name="prefix" value="/WEB-INF/views/"></property>
17     <!-- 后缀 -->
18     <property name="suffix" value=".jsp"></property>
19 </bean>
20 
21 </beans>

4,前台页面,发送请求 index.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>主页</title>
 8 </head>
 9 <body>
10 
11 <a href="hanqi/hello">发送请求</a>
12 
13 <form action="hanqi/hello" method="post">
14 
15 <input type="submit" value="提交POST请求">
16 </form>
17 <!-- REST风格 put/delete动作加隐藏域实现 -->
18 <form action="hanqi/helloDelete/123" method="post">
19 <input type="hidden" name="_method" value="DELETE">
20 <input type="submit" value="提交DELETE请求">
21 </form>
22 
23 <!-- 属性自动装入对象,并支持级联 -->
24 <form action="hanqi/hellouser" method="post">
25 用户ID:<input type="text" name="id"><br>
26 用户名称:<input type="text" name="username"><br>
27 
28 部门ID:<input type="text" name="bumen.bumenid"><br>
29 部门名称:<input type="text" name="bumen.bumenmc">
30 
31 <input type="submit" value="提交user信息">
32 </form>
33 <br>
34 
35 <div>
36 <a href="hanqi/helloP?id=123&name=hh">发送带参数的请求</a>
37 </div>
38 
39 <div>
40 <a href="hanqi/helloP?id=123">发送带选参数的请求</a>
41 </div>
42 
43 <div>
44 <a href="hanqi/helloS/123">发送带占位符参数的请求</a>
45 </div>
46 
47 
48 </body>
49 </html>

 

5,创建控制器类 HelloWorld.java

 1 package com.hanqi;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.PathVariable;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RequestMethod;
 7 import org.springframework.web.bind.annotation.RequestParam;
 8 
 9 //请求处理类 控制器类
10 @Controller
11 @RequestMapping("/hanqi")
12 public class HelloWorld {
13     
14     @RequestMapping(value="/hello")//前台请求路径
15     public String sayHello()
16     {
17         System.out.println("hello!world!");
18         
19         return "success";//返回的页面
20     }
21     
22     @RequestMapping(value="/helloP")
23     public String sayHelloP(@RequestParam("id") int id, @RequestParam(value="name",required=false,defaultValue="ttt") String username)
24     {
25         System.out.println("hello!参数:id="+id+"name="+username);
26         
27         return "success";
28     }
29     
30     //接收Spring servlet自动装配好的对象
31     @RequestMapping(value="/hellouser", method=RequestMethod.POST)
32     public String sayHellouser(User user)
33     {
34         System.out.println("hello!参数:id="+user);
35         
36         return "success";
37     }
38     
39     @RequestMapping(value="/helloS/{id}", method=RequestMethod.POST)
40     public String sayHelloS(@PathVariable("id") int id)
41     {
42         System.out.println("hello!参数:id="+id);
43         
44         return "success";
45     }
46     
47     //占位符 REST风格
48     @RequestMapping(value="/helloDelete/{id}", method=RequestMethod.DELETE)
49     public String sayHelloD(@PathVariable("id") int id)
50     {
51         System.out.println("hello!delete参数:id="+id);
52         
53         return "success";
54     }
55     
56     @RequestMapping(value="/hello", method=RequestMethod.POST)
57     public String sayHelloPost()
58     {
59         System.out.println("hello!POST");
60         
61         return "success";
62     }
63 
64 }

6,处理返回信息的页面 success.jsp 所在目录即springmvc-servlet.xml视图解析器前缀,习惯为/WEB-INF/views/,只有服务器内部程序可以访问,安全性高.

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>成功</title>
 8 </head>
 9 <body>
10 返回成功!
11 </body>
12 </html>

7,其他:用于演示的实体类 User.java Bumen.java

 1 package com.hanqi;
 2 
 3 public class User {
 4 
 5     private int id;
 6     private String username;
 7     private Bumen bumen;
 8     
 9     @Override
10     public String toString() {
11         return "User [id=" + id + ", username=" + username + ", bumen=" + bumen + "]";
12     }
13     public int getId() {
14         return id;
15     }
16     public void setId(int id) {
17         this.id = id;
18     }
19     public String getUsername() {
20         return username;
21     }
22     public void setUsername(String username) {
23         this.username = username;
24     }
25     public Bumen getBumen() {
26         return bumen;
27     }
28     public void setBumen(Bumen bumen) {
29         this.bumen = bumen;
30     }
31     
32 }
 1 package com.hanqi;
 2 
 3 public class Bumen {
 4     
 5     private int bumenid;
 6     private String bumenmc;
 7     
 8     @Override
 9     public String toString() {
10         return "Bumen [bumenid=" + bumenid + ", bumenmc=" + bumenmc + "]";
11     }
12     public int getBumenid() {
13         return bumenid;
14     }
15     public void setBumenid(int bumenid) {
16         this.bumenid = bumenid;
17     }
18     public String getBumenmc() {
19         return bumenmc;
20     }
21     public void setBumenmc(String bumenmc) {
22         this.bumenmc = bumenmc;
23     }
24 
25 }