前邊已經介紹過了Struts在Idea上的配置,相對於Struts來講,我以爲SpringMVC有更多的優點,首先Struts是須要對action進行配置,頁面發送不一樣的請求,就須要配置不一樣的action,並在每一個action中指定對應的結果集,而後進行頁面跳轉,固然這也是符合邏輯的,可是當要開發一個大的項目,會有不少請求,若是每一個請求都要對應一個action,這樣會比較繁瑣,但對於SpringMVC來講,不用配置多個action,並且SpringMVC框架與Spring有很好的對接,這兩個框架能夠實現無縫對接,下邊就給你們詳細介紹一下SpringMVC配置。html
對於Struts框架,你們能夠參考:http://www.cnblogs.com/wyyDemoTest/p/8676782.html
java
1、環境搭建(File-->New-->Project-->Spring,選中SpringMVC,其餘默認,點擊Next)web
2、填寫項目名字,建立項目完成,整個項目環境大部分已經完成spring
3、項目配置spring-mvc
你們先看一下整個的項目目錄服務器
其實,SpringMVC的配置很簡單的,下邊分步驟給你們接收一下,怎樣配置,在作下邊的配置以前,你們還須要配置服務器,詳細的介紹你們能夠參考Struts框架怎樣配置服務器:mvc
http://www.cnblogs.com/wyyDemoTest/p/8676782.htmlapp
1 首先先要配置index.jsp頁面,代碼以下,主要是頁面發送請求,到達服務器,進行後續邏輯判斷:框架
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登陸頁面</title> </head> <body> <form action="${pageContext.request.contextPath}/test/demo" method="post" > username:<input type="text" required="required" name="username"/><br> password:<input type="password" required="required" name="password"/><br> <input type="submit" value="提交"><br> </form> </body> </html>
2 配置web.xml文件,通常若是是運用Idea開發工具進行SpringMVC框架開發的話,系統會自動配置該配置文件,可是也須要進行相應的修改,尤爲是url:jsp
<?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_3_1.xsd" version="3.1"> <!--<listener> <listener-class>com.listener</listener-class> </listener>--> <!--配置Spring--> <!--配置SpringMVC--> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3 配置spring-mvc-servlet.xml,須要注意的是,有兩種方法對該文件進行配置,主要是該文件的文件名「spring-mvc-servlet.xml」
方法① 若是是按照步驟2中的代碼進行配置,則該配置文件的名字必須是spring-mvc-servlet.xml,其中「spring-mvc」是web.xml文件中的servlet name,這兩個名字必須一一對應:xxx-servlet.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_3_1.xsd" version="3.1"> <!--<listener> <listener-class>com.listener</listener-class> </listener>--> <!--配置Spring--> <!--配置SpringMVC--> <servlet> <servlet-name>demo</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>demo</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
該方法不用指定servlet的name與配置文件的名字一致,只須要指定相應的文件便可。
下邊是該配置文件的內容,我用的是註解的方法
<?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"/> <!-- 靜態資源(js、image等)的訪問 --> <mvc:default-servlet-handler/> <!-- 開啓註解 --> <mvc:annotation-driven/> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
4 配置控制器
package com; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; /** * Created by admin on 2018/3/26. */ @Controller @RequestMapping("/test") public class HelloController { @RequestMapping(value = "/demo",method= RequestMethod.POST) public String test(@RequestParam("username") String username, @RequestParam("password") String password, Model model){ model.addAttribute("name",username); model.addAttribute("password",password); model.addAttribute("test","123456"); return "hello"; } }
須要注意的是該配置器指定的@RequestMapping,必須和index.jsp的請求一致,這樣纔會到達該控制器,而後對數據進行邏輯判斷,固然你們能夠看一下控制器是如何獲取前臺頁面數據,當拿到前臺頁面數據之後,能夠對數據進行處理,增刪改查邏輯判斷,知足條件之後怎樣處理,頁面跳轉攜帶數據,把數據保存在Model對象中,前臺頁面經過el獲取數據。
5 hello.jsp,主要是顯示數據,在步驟4 中返回的字符串對應指定的字符串.jsp頁面,這樣整個流程都完成了
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>測試</title>
</head>
<body> ${name},${password} </body>
</html>