1、配置文件spring_mvc.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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="cn.your_name.controllers.controllers">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 视图解析器 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 开起SpringMVC框架注解支持 -->
<mvc:annotation-driven/>
</beans>
2、web.xml
<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>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 关联springmvc的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring_mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<!-- 启动服务器的时候,init dispathcerservlet,其中就加载xml文件-->
</servlet>
<!-- 这个servlet-mapping的作用是将名为"springmvc"的servlet映射到根URL("/")上。
这意味睽当用户访问网站的根URL时,该servlet将处理请求并返回相应的内容。
对于`localhost:8080/`,由于`<url-pattern>/</url-pattern>`将servlet映射到根URL("/")上,因此该servlet将处理根URL的请求。
对于`localhost:8080/index.jsp`,由于该URL不匹配servlet-mapping中的URL模式,
因此该请求【不会由该servlet处理】,而是【由其他适合的servlet】或者【直接返回index.jsp文件】。-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3、controller.java
@Controller
//@RequestMapping("/")
public class HelloController {
@RequestMapping("/hello")
public String hello(@RequestParam("name") String name)
{
System.out.println("hello springmvc"+name);
return "index";
}
/*
* 当 hello 方法被调用时,会打印出 "hello springmvc" + name 的信息,并且通过返回 "redirect:index.jsp" 来将请求重定向到 index.jsp 页面。
* */
/*
* 处理请求参数:
* servlet:
* request.getParamter("name")...
* springmvc中:自动接收参数+自动转换类型
* Integer name:先获取+不为null+类型转换
* 参数名要一zhi
* --会自动发送一个新的请求到 index.jsp 页面,继续渲染和显示该页面的内容
* --参数卫传入自动传入null
* 如果请求的参数跟处理方法的参数名不一致:@RequestParameter,用了就必须得传入参数,but:required = false,就不必,没有传入会自动传入null
* 因此参数类型不可以是基础类型
*defaulVaule="abc"
* */
@RequestMapping("/params01")
public String params01(@RequestParam(value="name",required = false, defaultValue="abc") String name)
{
System.out.println(name);
return "index";
}
@RequestMapping("/params02")
public String params02(@RequestParam(value="user",required = false) User user)
{
System.out.println(user);
return "params";
}
}
截图:
本文重点
1、
发现去掉【视图解析器】后,会报错,和【开起SpringMVC框架注解支持】没有关系
2、
视图解析器这里的路径问题
<property name="prefix" value="/WEB-INF/"/> <property name="suffix" value=".jsp"/>
+
controller类前的@RequstMapping ()的值
对请求路径url的影响
视图解析器:
将控制器(Controller)所产生的逻辑视图名称(Logical View Name)转换成实际的视图(View)对象。
prefix 属性指定了视图文件的路径前缀,suffix 属性指定了视图文件的后缀名。这里我们指定了 JSP 文件作为视图文件,因此后缀名为 “.jsp”。
使用 Spring MVC 进行 Web 开发时,我们通常会在控制器的方法中返回逻辑视图名称,eg:return “index”;
视图解析器配置了前缀和后缀两个属性,因此控制器类的视图路径仅需提供文件名,视图解析器将会自动添加前缀和后缀。
@RequestMapping
这个注解用来指定控制器可以处理哪些请求,其所做的工作相当于在web.xml中配置了servlet完成映射
- 添加在类上时,会将某个特定的请求映射到某个控制器上,例如localhost:8080/user就会定位到HelloController上,但是作用在类上的注解需要和方法上的路径组合才可一个可访问路径
- 若spring_mvc.xml中的视图解析器的前缀少了WEB前面的/:
>
这三张图片来源:https://www.jianshu.com/p/e2a5f82b6424
补充:
@GetMapping和@PostMapping是组合注解,分别是@RequestMapping(method =
RequestMethod.GET)和@RequestMapping(method = RequestMethod.POST)的缩写。作者:模哥 链接:https://www.jianshu.com/p/967b1ed65110 来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
GET方法:客户端与服务端之间是读取操作(如搜索、读、查询操作)。
POST方法:
1.交互是一个命令或订单(order),比提问包含更多信息
2.交互改变了服务器端的资源并被用户察觉,例如订阅某项服务
3.用户需要对交互产生的结果负责
根据HTTP协议规定,GET方法可以携带交互需要的所有数据,因此你会看到搜索百度或谷歌的时候,点击搜索形成的URL包含了你刚才的搜索关键字,没有安全需求的请求把信息放URL里没关系,但是你访问银行网站的时候,不希望把账户、密码这些放在URL里被人拦截是吧,所以HTTP设计了POST请求,他可以把请求信息放在HTTP请求里,具体格式这里不细说了,这样你就不能简单的从URL里找到账户、密码了
作者:模哥
链接:https://www.jianshu.com/p/967b1ed65110
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
例如:
@GetMapping("/params02/{name}")
public String params02(@PathVariable String name)
{
System.out.println(name);
return "params";
}