文章目录
一、JavaEE常用框架文章推荐
1.Spring:企业级 Java 应用程序开发框架
https://www.w3cschool.cn/wkspring/
2.SpringMVC:Web端框架
https://www.w3cschool.cn/wkspring/9pf81ha5.html
3.Struts2:Web端框架
https://www.w3cschool.cn/struts_2/
4.MyBatis :数据库框架
https://www.w3cschool.cn/mybatis/
5.Hibernate:数据库框架
https://www.w3cschool.cn/hibernate/
6.log4j :日志采集
https://www.w3cschool.cn/java/log4j-installation.html
7. SSM与SSH
SSM:Spring SpringMVC MyBatis
SSH:Spring Struts2 Hibernate
目前有部分公司采用新型的SSH
Spring SpringMVC Hibernate
二、SpringMVC与Struts的区别
1.struts1基于Servlet,struts2基于Filter,Spring基于servlet
2.struts1的action是单例模式,线程不安全,
struts2的action是多例模式,每次访问创建对象以保证线程安全
SpringMVC是单例模式,线程安全
...
URL匹配规则
1. 精确匹配
<servlet-mapping>
<url-pattern>/user/addUser.action</url-pattern>
</servlet-mapping>
http://localhost:8080/appDemo/user/users.html
2. 路径匹配
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/user/*</url-pattern>
</servlet-mapping>
http://localhost:8080/appDemo/user/users.html
http://localhost:8080/appDemo/user/addUser.action
http://localhost:8080/appDemo/user/updateUser.actionl
3. 扩展名匹配
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
http://localhost:8080/appDemo/user/users.jsp
http://localhost:8080/appDemo/toHome.action
4. 缺省匹配
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
三、创建第1个SpringMVC框架:hello world
1.添加jar包
2.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>SpringMVCDemo01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>aplicationContext</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>aplicationContext</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
3.配置核心文件xxx-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 1.约束文件 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
</beans>
4.创建Controller
package com.zhiyou100.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class TestController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
ModelAndView modelAndView =new ModelAndView();
modelAndView.setViewName("1");
return modelAndView;
}
}
5.创建JSP
6.在xxx-servlet.xml文件中添加映射
<bean name="/hello.do" class="com.zhiyou100.controller.HelloController"></bean>
7.测试
http://localhost:8080/springmvc_01/hello.do
四、视图解析器
<bean id="jspView"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
五、原理
1.http://localhost:8080/springmvc_01/home.do
2.执行到web.xml 判断URL
3.如果是执行到框架里面--XXX-servlet.xml
4.通过HandlerMapping处理映射器找name叫做 home.action的bean
5.执行到对应的类里面handleRequestInternal方法里面
6.return视图 又回到xxx-servlet.xml里面
7.对视图进行拼接
8.执行到对应的jsp页面 --回到了浏览器
六、HandlerMapping处理映射器
6.1 name:BeanNameUrlHandlerMapping
默認的 通過beanName
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
6.2 controller:ControllerClassNameHandlerMapping
根據類名來的 如果類名後面的controller.可以省略不寫; 首字母小寫
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean>
6.3 url:SimpleUrlHandlerMapping
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/a.do">home</prop>
<prop key="/b.do">home1</prop>
</props>
</property>
</bean>
<bean id="spring" class="com.zhiyou100.controller.SpringController">
</bean>
6.4 Annotation:DefaultAnnotationHandlerMapping
七、核心配置文件
7.1 默认的名称规范
默认根据DispatcherServlet的name去拼接xxx-servlet.xml
例如:
hello:hello-servlet.xml
aplicationContext:aplicationContext-servlet.xml
springMVC:springMVC-servlet.xml
7.2 指定文件名:仍然放在WEB-INF下
<servlet>
<servlet-name>aplicationContext</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/springMVC-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>aplicationContext</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
7.3 指定文件名:放在src下
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC-servlet.xml</param-value>
</init-param>
八、注解:代替配置
<?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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
<mvc:annotation-driven/>
<context:component-scan base-package="com.zhiyou100"></context:component-scan>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
九、 Controller
package com.zhiyou100.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HController {
@RequestMapping("/1.do")
public String m1() {
return "1";
}
@RequestMapping("2.do")
public String m2() {
return "2";
}
@RequestMapping("3.do")
public String m3() {
return "3";
}
}
十、获取参数
10.1.获取表单或者超链接里面的参数
@RequestMapping("2.do")
public String m2(String username,Integer age) {
System.out.println(username+":"+age);
return "2";
}
10.2.表单里有多个参数, 这些参数刚好和model一样;
@RequestMapping("3.do")
public String m3(User user) {
return "3";
}
10.3.表单里面的参既有model的也有model里面没有的
@RequestMapping("4.do")
public String m3(User user,Integer age) {
return "3";
}
十一、传递参数
1.把参数传递会页面
@RequestMapping("/1.do")
public String m1(Model model) {
model.addAttribute("name", "哈哈啊哈");
return "1";
}
2.获取参数同时有传递参数
@RequestMapping("/1.do")
public String m1(String username,Model model) {
model.addAttribute("name", "哈哈啊哈");
return "1";
}
十二、返回内容
1.返回视图名称
@RequestMapping("/1.do")
public String m1(String username,Model model) {
model.addAttribute("name", "哈哈啊哈");
return "role/show";
}
2.返回文本
@ResponseBody
@RequestMapping("2.do")
public String m2(String username,Integer age) {
System.out.println(username+":"+age);
return "2";
}
3.返回JSON
springmvc-servlet.xml中添加
<mvc:annotation-driven />
@ResponseBody
@RequestMapping("3.do")
public Map<String, String> m3() {
Map<String, String> map = new HashMap<>();
map.put("name", "hahaha");
return map;
}
作业:
1.独立能配置出SpringMVC框架
2.使用SpringMVC进行CRUD