Eclipse中spring mvc配置執行個體詳解

來源:互聯網
上載者:User

1.建立動態網頁項目,並修改web.xml。


Web.xml內容如下:

<?xml version="1.0"encoding="UTF-8"?> 

<web-app version="2.5"  

    xmlns="http://java.sun.com/xml/ns/javaee"  

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  

   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

     

    <servlet> 

     <servlet-name>dispatch</servlet-name> 

     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 

     <init-param> 

      <param-name>contextConfigLocation</param-name> 

      <param-value>/WEB-INF/spring-servlet.xml</param-value>  <!-- servlet.xml所在路徑 -->

     </init-param> 

     <load-on-startup>1</load-on-startup> 

    </servlet> 

    <servlet-mapping> 

     <servlet-name>dispatch</servlet-name> 

     <url-pattern>*.do</url-pattern> 

    </servlet-mapping> 

  <welcome-file-list> 

    <welcome-file>index.jsp</welcome-file> 

  </welcome-file-list> 

</web-app> 

2.建立servlet.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-3.0.xsd 

       http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd 

       http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

        

  <context:component-scan base-package="logintest"></context:component-scan>   <!-- action所對應的java控制類所在包名--> 

        <mvc:annotation-driven /> 

        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 

    <property name="prefix"value="/WEB-INF/jsp/"/>  <!-- 處理請求後轉向檔案所在位置 --> 

    <property name="suffix"value=".jsp"/>           <!-- 檔案的尾碼也可為ASP html等 --> 

    <property name="viewClass" 

            value="org.springframework.web.servlet.view.JstlView"/> 

    </bean> 

  </beans>

3.建立登陸頁面

<form action="login.do"method="post">

    <input type="text"name = "username" ><p>

    <input type="password"name = "password" ><p>

    <input type="submit"value="submit">

</form>

4.匯入jar包,匯入jar包很關鍵。大都是以RELEASE.jar結尾的jar包,全部匯入也有可能出錯。Jar包版本的選擇也要考慮與jdk與tomcat版本。Jdk1.8要考慮spring4.0及以上。


5.建立control類

 

packagelogintest; //與servlet.xml檔案中對應的包名要對應

 

importorg.springframework.stereotype.Controller; 

importorg.springframework.web.bind.annotation.RequestMapping; 

@Controller //表示為控制類

public class logincontrol {

    @RequestMapping("login")  //對應form表單 action中的內容

    public Stringlogin(String username,String password){

        if ("test".equals(username)) {

            System.out.println(username +" 登入成功");

            return"Success";//邏輯視圖名       跳轉頁面預設為轉寄     

        }

        return"Error";

    }  

}

 

6.建立Success.jsp 與Error.jsp 

 

不再贅述。

7、測試結果



下載原始碼連結:http://download.csdn.net/detail/u011489887/9731973

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.