Spring MVC 是Spring 提供的一个实现web MVC 设计模式的轻量级WEB框架。
它与Struts2框架一样,都是MVC框架,性能更加优于Struts2
Spring MVC 特点:
是SPring框架的一部分,可以便利用Spring所能提供的其他功能。
灵活性强,抑郁与其他框架集成。
提供了一个前端器DispatcherServlet ,是开发人员无须额外开控制器对象。
可自动绑定用户输入,并能正确的转化数据类型。
内置了常见的效验器,可以效验用户输入。如果效验不能通过,那么会重定向到输入表单。
支持国际化。可以根据用户区域显示多国语言。
支持多种视图技术。它支持JSP、Veiclity 和FreeMaker等视图技术
建立的方式有两种一种是通过导包的方式一种是通过maven的方式创建的
第一种方式:
1.导ar包
放在lib里,不要忘记
创建完的目录
userConter
package cn.javabs.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("user")
public class UserConter {
// 控制翻转
@RequestMapping("add")
public String addUser(){
System.out.println("假设添加成功了");
return "success";
}
}
UserServlvlet:
package cn.javabs.controller;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "UserServlet")
public class UserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--扫包注解-->
<context:component-scan base-package="cn.javabs"/>
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/view/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
success.jsp
<%--
Created by IntelliJ IDEA.
User: love
Date: 2020/5/7
Time: 15:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>我是view里success页面里的成功</h2>
</body>
</html>
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">
<!-- 配置前端控制器 springmvc.xml 在哪呢 干什么的? -->
<servlet>
<servlet-name>dispatcherServlet</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-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
<%--
Created by IntelliJ IDEA.
User: love
Date: 2020/5/7
Time: 8:18
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<span>路径没有写</span>
<hr/>
<a href="${pageContext.request.contextPath}/user/add">点我去后台执行代码</a>
</body>
</html>
main.jsp
<%--
Created by IntelliJ IDEA.
User: love
Date: 2020/5/7
Time: 15:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>进入main页面</h2>
</body>
</html>
执行中错误解析:
点击运行还报错,错误是
把jar包放入,执行后的错
解决方法:
此时后台输出了这句话,说明成功了,单页面有问题,解决方法
运行后,路径有点问题,解决方法:
运行后:
maven只能访问到index,同时不用需页面跳转页面
流程图