1.首先创建一个web工程
2.删除src文件夹(因其不符合springMVC的开发要求)
3.在Java Resource下创建如图的文件夹
4.然后将下面四个包导入WebContent/WEB-INF/lib中(这四个是spring的核心jar包)
后来继续导入如下三个包(高亮的 这三个包是与Javaweb相关的包)
5.然后在src/main/resource目录下面创建一个spring Bean Configuration File 文件(命名任意但要符合规范)文件并做如下步骤
6.我们在app.xml中(我创建的spring Bean Configuration File文件叫这个)配置Namespaces(勾选如下选项)Namespaces在打开的spring Bean Configuration File文件的下方就能找到这个选项
7.配置web.xml(在web.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:app.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
一旦配置完load-on-startup就会在这个工程发布时创建这个servlet
init-param中的配置为了定位app.xml
DispatcherServlet在如下的目录中
8.创建测试用的jsp文件(结构如下图所示,fali、success在body中写一个no 和 ok 就行表示,失败和成功)
9.写index.jsp(index用于跳转到login.jsp 注意:在WEB-INF 文件夹内的文件只能用请求转发的方式访问而不能直接进行访问)和login.jsp中的代码
下面我将两段代码展示出来
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
session.setAttribute("path", path);
%>
<script type="text/javascript">
location.href="${path}index/index.do";
</script>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
session.setAttribute("path", path);
%>
<html>
<head>
<title>login</title>
</head>
<body>
<form action="${path }userinfo/login.do" method="post">
<input name="userName"/>
<input type="password" name="password"/>
<input type="submit" value="登陆"/>
</form>
</body>
</html>
由于我在jsp页面中使用了java代码,所以需要引入如右图一个包
至于location.href与from表单中的action中的代码是什么含义我会在后面再解释,这里我可以告诉大家${path }是使用的El表达式来获取path的值,这个path值就是上面java代码中的那个path,Java代码中的path的值为绝对路径,路径到项目名为止
图片!!!!
10.构建MVC结构(创建如下的java包及java文件)
11.配置spring中的注解
在Controller上加上@Controller 在Service(注意是Service的实现类)上加@Service、Dao(也是Dao的实现类)上加@Repository或者@Component
12.配置app.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.3.xsd
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-4.3.xsd">
<context:component-scan base-package="com.demo"></context:component-scan>
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
①.component-scan 内 base-package属性设置了com.demo 这行代码设置com.demo自动扫描
②.mvc:default-servlet-handler是配置那些没有配置controller的页面,让其自动让tomact自动处理
③.下面bean内的内容是配置跳转页面的前缀和后缀
13.引入数据库工具类
14.为功能创建方法并且添加注解,注解的参数与跳转的地址相同(去掉文件后缀和项目地址),如果要设置提交的方式则在注解中添加method参数(仅展示了login功能的代码)
最后测试搭建完毕