實現簡單的Spring MVC登陸(不帶數據庫)

這項目不是用maven,是創建Dynamic Web Project


步驟:

1.添加jar包

2.配置web.xml(WebContent/WEB-INF目錄下)

3.配置springmvc-servlet.xml(名字與 web.xml 裏 <servlet-name> 相同,加上 -servlet)

4.創建建議登陸頁面

5.後臺搭建控制類@Contorller


詳解:

1.添加jar包

spring包從官網下載:http://projects.spring.io/spring-framework/

現在spring官網推薦的是maven添加,所以要下載包:http://repo.spring.io/release/org/springframework/spring/

另外還需要:commons-logging-1.2.jar 和 jstl.jar (不添加會報錯。。)

下載地址得自己找,我這有個網址:http://mvnrepository.com/

。。好像不是這個,在我公司電腦裏,到時候補充。這個網址主要是maven添加包的寫法,還是挺有用的。



2.配置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"> 

     
<!-- 添加spring控制器和映射 -->
    <servlet> 

     <servlet-name>springmvc</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> 

<!-- 攔截所有以do 結尾的請求 -->  
    <servlet-mapping> 

     <servlet-name>springmvc</servlet-name> 

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

    </servlet-mapping> 

  <welcome-file-list> 

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

  </welcome-file-list> 

</web-app> 

<web-app>

文件約束,直接copy就行


<servlet></servlet>:

<servlet-name> 這個是我們要註冊servlet的名字,一般跟Servlet類名有關

<servlet-class>  這個就是指向我們要註冊的servlet 的類地址, 要帶包路徑

<init-param> 這個是需要初始化的參數,這是第三方提供的,它只聲明的必須的變量並給了個默認值保證他的完整,而此時的默認值不是你需要的,可以用init-param來改變它。

 <load-on-startup>1</load-on-startup>是啓動順序,讓這個Servlet隨Servletp容器一起啓動。


<servlet-mapping>:

<servlet-name>springmvc</servlet-name>這個Servlet的名字是springmvc,可以有多個DispatcherServlet,是通過名字來區分的.

<url-pattern>:會攔截*do結尾的請求


<welcome-file-list>:

在web項目中,當用戶在瀏覽器中輸入的URL不包含某個特定的Servlet、html、jsp頁面時,web.xml中<welcome-file-list>標籤元素就會指定顯示的默認文件。



3.配置springmvc-servlet.xml(名字與 web.xml 裏 <servlet-name> 相同,加上 -servlet)

<?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/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"> 

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


<!-- 啓用spring mvc 註解 --> 
  <mvc:annotation-driven /> 

<!-- 定義跳轉的文件的前後綴   -->  
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 

	    <property name="prefix" value="/"></property>
	    <property name="suffix" value=".jsp"/>
	    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 

  </bean> 

  </beans>

問題:如將 <property name="prefix" value="/" /> 配置爲 <property name="prefix" value="/WEB-INF" /> 總是出現找不到。但路徑是沒有問題的。

總會出現“The requested resource (/springmvcdemo1/user/success.jsp) is not available.”這個問題


4.創建建議登陸頁面(自己設置),還有跳轉成功、失敗頁面。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="login.do" method="post">

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

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

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

</form>
</body>
</html>

這裏需要注意:

<form action="login.do">

 action裏的名稱 'login' 要與控制類裏@RequestMapping的名稱一致

'.do'要與配置文件web.xml裏<servlet-mapping>下的<url-pattern>一致


success:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="UTF-8"%>
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
success ${username}
</body>
</html>


5.後臺搭建控制類@Contorller

注意:包名要與<context:component-scan>一致

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

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

//@Controller註解標識一個控制器
@Controller
public class logincontrol {

	@RequestMapping("login")  //@RequestMapping註解標記一個訪問的路徑

    public ModelAndView login(String username,String password,HttpServletRequest request){

        if("admin".equals(username) && "admin".equals(password)){  
            request.setAttribute("username", username); 
            return new ModelAndView("/Success");  
        }  
        return new ModelAndView("/Error");//return返回頁面地址
    }  
}


記得一定要一一對應。。我剛剛重做一遍,報錯tomcat:Server Tomcat v7.0 Server at localhost failed to start.

因爲配置錯誤,所以報錯。


參考文章:http://blog.csdn.net/caikule/article/details/17076057

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章