1:若不用maven方式创建,则先构建Dynamic web,然后在lib中导入
这几个包,一个不能少,然后build path
2:在web.xml中配置
<servlet>
<servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class></servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>3:在WEB-INF下建立springmvc-servlet.xml,然后在springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> //配置返回文件的前缀和后缀<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property> (<property name="prefix" value="/WEB-INF/my"></property>) <property name="suffix" value=".jsp"></property> </bean> <mvc:default-servlet-handler/>(指定springmvc中的处理不了的事让tomcat处理) <mvc:annotation-driven/>(指定让springmvc处理更高级的事) <context:component-scan base-package="com.atguigu.shiro" /></beans>4:在制定的com.atguigu.shiro下新建java
@RequestMapping("/springmvc")
@Controllerpublic class helloword { @RequestMapping(value="/hello",method=RequestMethod.GET) public String hello(){ System.out.println("111"); return "index"; }}
5:在指定的文件夹WEB-INF下创建index.jsp
6:@RequestMapping的params和headers的学习
访问时候直接?username=yyy&age=1 访问即可
7:restful风格代码
(1)
@RequestMapping("/testPathVariable/{id}")
@PathVariable(value="id" Integer id)注意@PathVariable中的value要和@RequestMapping中的{}中的内容一致,也就是{id}和value="id"
(2)增删改查前端页面写
后台写如下图所示即更新和删除
8:@RequestParam
required是否为必须要传入的,int age如果不传入数据那么age==null,但是int型在此存放null会报错,因此要给一个默认值为0,如果不给默认值可以用Integer age
9:响应请求头消息
10:@CookieValue
11:POJO方式最常用,springmvc会按照请求参数名和pojo属性名进行自动匹配
12:获取request、write、InputStream、OutputStream、reader、httpSession等数据,
13:返回ModelAndView,例如ModelAndView是一个jsp,然后将数据绑定到jsp上
return modelAndView;
success.jsp页面可以获取到time的值
14:目标方法也可是一个map,将数据存入到map中,该方式是把map放入了request的域中了
要获取names的值,可以是names:${requestScope.names}获取
15:想要把map放入session中,定义一个map并且在类名上面写一个@SessionAttributes。value指定map中键,
补充:
一:jstlView
https://www.cnblogs.com/zhoutiekui/p/6368465.html
二:mvc:view-controller 用法(参考网址https://www.cnblogs.com/caoyc/p/5637894.html)
WEB-INF下面有views文件夹,views文件夹下有hello.jsp,我在xml文件中已经配置好前缀是/WEB-INF/views,后缀是jsp,
所以要访问hello.jsp。path就写hello.jsp在项目中的相对路径就可以了(相对于WebContent的路径)
<mvc:view-controller path="/hello" view-name="hello"></mvc:view-controller>然后在返回的路径下就写path.jsp<a href="hello">Say</a>可以访问