sprimgmvc入门
springframework
springmvc
srpingboot
mvc
model 模型层 Book.java
view 视图层 show.jsp
controller 控制器层 /show.do
struts 1.x apache struts mvc
webwrok + struts = struts2 filter 拦截器
springmvc servlet
ssh springframework struts hibernate
ssm spring springmvc mybatis
struts 1.x springframework
springmvc框架
struts 2.x struts + webwork = struts2
ssh spring struts hibernate
ssm spring springmvc mybatis
struts
action
show.action
springmvc 就是servlet + 视图渲染
controller 控制器
show.do
show.action
show
mvc 框架
Model 模型层 模型bean类
View 视图层 jsp el jstl
Controller 控制器层 servlet
java mvc框架 必须web项目
1) struts1.x
2) struts2.x struts+webwork 新框架 拦截器(过滤器)
3) springmvc
servlet
springmvc入门
1、建立maven项目的web项目
2、pom.xml文件四入
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>
3、修改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>smvc01</display-name>
<!-- springmvc servlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 会自动加载/WEB-INF/springmvc-servlet.xml springmvc框架配置文件 -->
<!-- 如下配置,会自动加载src/main/resources/springmvc*.xml配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- welcome home -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4、
在resources/建立springmvc.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:p="http://www.springframework.org/schema/p"
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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- HandlerMapping HelloController 可以直接hello.do hello.action 根据类名来查找映射 -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<!-- 配置handerAdapter 适配器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- 处理映射器 根据bean name 名称进行查找 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!-- 配置视图渲染器 -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!-- 将视图名 渲染后视图的前缀 -->
<property name="prefix" value="/"/>
<!-- 渲染后视图的后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- 简单处理映射器 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/a.action">a</prop>
<prop key="/b.action">a</prop>
<prop key="/c.action">a</prop>
</props>
</property>
</bean>
<!-- 控制器 处理器 -->
<!-- test.do test.action user.action a.action b.action c.action-->
<bean class="com.fz.controller.TestController" id="a" name="/t.action"/>
<bean class="com.fz.controller.Hello"/>
<!-- 引入其它配置文件 -->
<import resource="my.xml"/>
</beans>
在resources/建立my.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:p="http://www.springframework.org/schema/p"
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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean class="com.fz.controller.UserController" name="/uu.action"/>
</beans>
5、编写控制器类
src/main/java/com/fz.controller/TestController.java
package com.fz.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
/**
* Created by webrx on 2017-06-24.
*/
public class TestController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception {
res.setCharacterEncoding("utf-8");
res.setContentType("text/html;charset=utf-8");
PrintWriter out = res.getWriter();
out.print("<h3>Test-SpringMVC-world--中文效果</h3>");
out.flush();
out.close();
System.out.println("hello-test");
return null;
}
}
package com.fz.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
/**
* Created by webrx on 2017-06-24.
*/
public class Hello implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception {
res.setCharacterEncoding("utf-8");
res.setContentType("text/html;charset=utf-8");
PrintWriter out = res.getWriter();
out.print("<h3>Hello123</h3>");
out.flush();
out.close();
System.out.println("hello-hello");
return null;
}
}
package com.fz.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Created by webrx on 2017-06-24.
*/
public class UserController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception {
ModelAndView mv = new ModelAndView("user");// 项目根目录下 /user.jsp
mv.addObject("name","张三丰");
mv.addObject("addr",new String[]{"郑州","北京","天津"});
mv.setViewName("WEB-INF/show"); //找/WEB-INF/show.jsp 渲染 /WEB-INF/show.jsp
return mv;
}
}
6、部署项目测试
http://localhost/test.do
http://localhost/user.do
http://localhost/uu.action
springmvc注解入门
1、建立maven web项目
2、项目pom.xml 添加
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
3、编写servlet3 注解类 相当于web.xml配置
package com.fz.servlet;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
/**
* Created by webrx on 2017-06-24.
*/
@WebServlet(name = "springmvc", urlPatterns = {"/"}, loadOnStartup = 1,
initParams = {@WebInitParam(name = "contextConfigLocation",
value = "classpath:smvc.xml")})
public class SpringServlet extends DispatcherServlet {
}
4、在src/main/resources/smvc.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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.fz.controller" />
<!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
5、编写控制器
package com.fz.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by webrx on 2017-06-24.
*/
//注解当前类为控制器类
@Controller
public class Hello {
//控制器映射方法 可以访问 http://localhost/show.action
//@ResponseBody 相当于不使用模板渲染,直接在当前输出
@RequestMapping("/show.action") @ResponseBody
public void show(){
System.out.println("show.......");
}
@RequestMapping("/user") @ResponseBody
public void user(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.setCharacterEncoding("utf-8");
res.setContentType("text/html;charset=utf-8");
PrintWriter out = res.getWriter();
out.print("Hello-springmvc");
out.flush();
out.close();
System.out.println("show.......");
}
@RequestMapping("/test")
public ModelAndView test(){
ModelAndView mv = new ModelAndView("test"); // /test.jsp
mv.addObject("name","李四四");
return mv;
}
}
6、直接运行测试
http://localhost/test
http://localhost/user
http://localhost/show.action
1、建立maven web项目
2、项目pom.xml 添加
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>
3、编写servlet3 注解类 相当于web.xml配置
package com.fz.servlet;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
/**
* Created by webrx on 2017-06-24.
*/
@WebServlet(name = "springmvc", urlPatterns = {"/"}, loadOnStartup = 1,
initParams = {@WebInitParam(name = "contextConfigLocation", value = "classpath:smvc.xml")})
public class SpringServlet extends DispatcherServlet {
}
4、在src/main/resources/smvc.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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.fz.controller" />
<!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
<?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="com.fz.controller" />
<!-- 在没有配置mvc:resources的时候没有问题,一旦配置了mvc:resources,注解方式的url就没有加载 -->
<mvc:annotation-driven>
<!-- 消息转换器 -->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/template/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
src/main/webapp/WEB-INF/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>springmvc02</display-name>
<!-- springmvc 5.0.6 servlet-->
<servlet>
<servlet-name>smvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 默认加载配置文件 /WEB-INF/smvc-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:smvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>smvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
5、编写控制器
package com.fz.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by webrx on 2017-06-24.
*/
//注解当前类为控制器类
@Controller
public class Hello {
//控制器映射方法 可以访问 http://localhost/show.action
//@ResponseBody 相当于不使用模板渲染,直接在当前输出
@RequestMapping("/show.action") @ResponseBody
public void show(){
System.out.println("show.......");
}
@RequestMapping("/user") @ResponseBody
public void user(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.setCharacterEncoding("utf-8");
res.setContentType("text/html;charset=utf-8");
PrintWriter out = res.getWriter();
out.print("Hello-springmvc");
out.flush();
out.close();
System.out.println("show.......");
}
@RequestMapping("/test")
public ModelAndView test(){
ModelAndView mv = new ModelAndView("test"); // /test.jsp
mv.addObject("name","李四四");
return mv;
}
}
6、直接运行测试
http://localhost/test
http://localhost/user
http://localhost/show.action