web.xml
<?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">
<!-- 第一步:web配置中的DispatcherServlet,所有请求/(不包含jsp文件),/*(所有,包含jsp文件)-->
<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>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 第二步-->
<context:component-scan base-package="com.rzgsl.controller"/>
<!-- 扫描包 相当于给controller配bean maping扫描 -->
<mvc:annotation-driven/>
<!-- 注解驱动-->
<mvc:default-servlet-handler/>
<!-- 默认配置,过滤静态资源,如果该请求已经作了映射,那么会接着交给后台对应的处理程序,如果没有作映射,就交给 WEB 应用服务器默认的 Servlet 处理,从而找到对应的静态资源,只有再找不到资源时才会报错。-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
HelloController.java
package com.rzgsl.controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@org.springframework.stereotype.Controller
public class HelloControllor{
@RequestMapping("/hello")
public String hello(Model model){
model.addAttribute("msg","hello my springmvc...");
return "test";
}
}
test.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
二、四个注解功能一样,只是为了区分类
@compont 组件
@service service
@controller controller
@repository dao
return的页面是可以复用的,带着不同数据的Attribute返回到同一个页面,页面展示的数据不同
三、RequestMaping(“。。。”),可以放在类或方法上;在类上加,相当于在类下面的方法增加了一层地址
四、Restful风格,全部都是“/”传参
五、@PathVariable
return “redirect:/index.jsp"
return "forward: /index.jsp"
package com.rzgsl.controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@org.springframework.stereotype.Controller
public class HelloControllor{
@RequestMapping("/hello/{a}/{b}")
public String hello(@PathVariable int a,@PathVariable int b,Model model){
int c = a + b;
model.addAttribute("msg",c);
return "test";
}
}
springmvc在web.xml中添加过滤器,如果还不行,检查tomcat的编码是否已修改,注意/* 带*才能过滤jsp
只要修改了xml文件,就必须得重启tomcat
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
@RestController 类上
@ResponseBody 方法
这两个都是返数据,不返视图
json是一种格式,前后端约定传输的一种方式,JavaScript object notation(对象标记语言),纯文本格式存储数据,
java,对象转成json的包,有Jackson
容易出错:1、类没有加有参,无参,getset(错误的话,弹出500错误)
2、Artifacts的WEB-INF中没有lib目录(没有的话Tomcat打包不成功,不会弹出首页)新增jar包后,还得在lib中加一遍
3、spring构架,必须有web.xml和Springmvc.xml配置才能生效,web.xml对应的是服务器web配置,springmvc.xml是java的配置