1.SpringMVC的注解开发
1)一个注解的列子
(1)注解需要的依赖包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- 配置ServletAPI依赖 -->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!--配置JSP依赖包-->
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<!--配置JSTL的依赖-->
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
(2)在web.xml文件中配置中央控制器
<servlet>
<servlet-name>dispatcherServle</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>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServle</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
(3)在resources下创建springmvc的配置文件
<?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:mvc="http://www.springframework.org/schema/mvc"
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/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">
<!-- 开启注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置自动扫描包-->
<context:component-scan base-package="com.springmv.controller"></context:component-scan>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"><property>
<property name="suffix" value=""></property>
</bean>
</beans>
(4) 创建控制类和请求处理方法
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
@Controller
public class HelloController {
@RequestMapping("/hello")
public ModelAndView helloReq(HttpServletRequest req){
String username=req.getParameter("username");
ModelAndView mav=new ModelAndView();
mav.addObject("myname",username);
mav.setViewName("test.jsp");
return mav;
}
}
(5)创建test.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>测试基于注解的SpringMVC程序====${myname}</h1>
</body>
</html>
(6)配置服务器运行测试
2.SpringMVC的注解开发
@Controller注解:表示我们所编写的java类是一个处理请求的控制器类。
只能作用在java类
可以使用@Component去代替,在javaweb程序中是分层出来的为了表名
@Controller中包含有@Component。
@Controller与我们在spring中学习的@Service和@Repository将应用程序标记为不同的层。
数据访问层:@Repository
业务访问层:@Service
Web层【控制层】:@Controller
@RequestMapping---设置请求处理类/请求处理方法的访问路径
位置:请求处理类:设置类的访问路径
请求处理方法:设置请求处理方法的访问路径
1)设置路径(value="路径")可以省略value
@Controller
public class TestController {
@RequestMapping("/testreq")
public void testReq(){
System.out.println("处理用户请求");
}
}
测试链接地址: http://localhost:8080/demo/one
服务器响应结果:
(1)?匹配任何单字符
设置注解
@RequestMapping("/?testreq")
链接地址:
http://localhost:8080/demo3/test/testreq [错误]
http://localhost:8080/demo3/test/Atestreq 【正确】
(2)** :匹配多个路径
设置注解
@RequestMapping("/**/testreq")
链接地址:
http://localhost:8080/demo3/test/testreq【正确】
http://localhost:8080/demo3/test/hello/testreq【正确】
http://localhost:8080/demo3/test/hello/abc/testreq【正确】
2)method限制请求方式
如果什么都不写那么就可以通过任何方式进行访问。
设置为get请求
java代码
测试链接地址: http://localhost:8080/demo/two
服务器响应结果:
设置为post请求
@RequestMapping(value = "/two",method = RequestMethod.POST)
页面结果:服务器不会响应
无奈源于不够强大