Java編程實(shí)現(xiàn)springMVC簡(jiǎn)單登錄實(shí)例

Spring MVC屬于SpringFrameWork的后續(xù)產(chǎn)品,已經(jīng)融合在Spring Web Flow里面。Spring 框架提供了構(gòu)建 Web 應(yīng)用程序的全功能 MVC 模塊。使用 Spring 可插入的 MVC 架構(gòu),從而在使用Spring進(jìn)行WEB開發(fā)時(shí),可以選擇使用Spring的SpringMVC框架或集成其他MVC開發(fā)框架,如Struts1,Struts2等。

從事重慶服務(wù)器托管,服務(wù)器租用,云主機(jī),虛擬主機(jī),域名注冊(cè),CDN,網(wǎng)絡(luò)代維等服務(wù)。

1.新建web項(xiàng)目:springmvc

2.導(dǎo)入springmvc需要的jar包

3.配置web.xml文件(核心代碼)

<servlet>
   <servlet-name>spmvc</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
   <servlet-name>spmvc</servlet-name>
  <url-pattern>*.do</url-pattern>   
 </servlet-mapping>

4.編寫index.jsp頁面(核心代碼)

<form action="login.do" method="post">
  username:<input type="text" name = "username" ><p> 
  password:<input type="password" name = "password" ><p>
  <input type="submit" value="登錄"> 
</form>

5.編寫loginSuccess.jsp 和 loginError.jsp 頁面 代碼略(隨意標(biāo)記下就是)

6.編寫java代碼(核心代碼)

@Controller
public class loginAction {
  
  @RequestMapping("login.do")
  public String login(String username,String password){
    if ("admol".equals(username)) {
      System.out.println(username +" 登錄成功");
      return "loginSuccess";//邏輯視圖名    跳轉(zhuǎn)頁面默認(rèn)為轉(zhuǎn)發(fā)

        System.out.println(username +" 登錄成功");
    }
    return "loginError";
  }  
}

注意:在導(dǎo)入ModelAndView包的時(shí)候是 導(dǎo)入servlet下的包。org.springframework.web.servlet.ModelAndView;
方法中的參數(shù)名必須和jsp頁面?zhèn)鬟f過來的name屬性名字一樣

7.配置spmvc-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:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    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-3.0.xsd">
  <!-- 啟用spring mvc注解 -->
  <context:annotation-config></context:annotation-config>
  <!-- 掃描包 -->
  <context:component-scan base-package="com.wjl.web"></context:component-scan>
  
  <!-- 對(duì)轉(zhuǎn)向頁面的路徑解析。prefix:前綴, suffix:后綴  如:http://127.0.0.1:8080/springmvc/jsp/****.jsp-->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/jsp/" p:suffix=".jsp"></bean>
</beans>

8.將項(xiàng)目發(fā)布到Tomcat服務(wù)器并運(yùn)行。

測(cè)試 結(jié)果:

Java編程實(shí)現(xiàn)springMVC簡(jiǎn)單登錄實(shí)例

Java編程實(shí)現(xiàn)springMVC簡(jiǎn)單登錄實(shí)例

使用其他方式傳遞:

/**
   * 返回的是一個(gè)ModelAndView
   * @param username 頁面?zhèn)鬟f的用戶名
   * @param password 頁面?zhèn)鬟f過來的密碼
   * @return
   */
  @RequestMapping("login2.do")
  public ModelAndView login2(String username,String password){
    if ("admol".equals(username)) {
      System.out.println(username +" 登錄成功2");
      return new ModelAndView("loginSuccess");//邏輯視圖名    跳轉(zhuǎn)頁面默認(rèn)為轉(zhuǎn)發(fā)
    }
    return new ModelAndView("redirect:/jsp/loginError");//以重定向的方式
  }
  
  /**
   * 傳遞一個(gè)JAVABEAN對(duì)象給控制器
   * @param users bean對(duì)象
   * @return
   */
  @RequestMapping(value="login3.do",method=RequestMethod.POST)
  public ModelAndView login3(@ModelAttribute("users") Users users){
    if ("admol".equals(users.getUsername()) && "123".equals(users.getPassword())) {
      System.out.println(users.getUsername() +"  "+ users.getPassword());
      return new ModelAndView("loginSuccess");
    }
    return new ModelAndView("redirect:/jsp/loginError.jsp");//以重定向的方式
  }

jsp頁面只需要改變下form表單的action就行。

Users.java

package com.wjl.bean;

public class Users {  
  private String username;
  private String password;
  public String getUsername() {
    return username;
  }
  public void setUsername(String username) {
    this.username = username;
  }
  public String getPassword() {
    return password;
  }
  public void setPassword(String password) {
    this.password = password;
  }

}

測(cè)試結(jié)果就不寫了。

踏實(shí)一些,不要著急,你想要的,歲月都會(huì)給你

總結(jié)

以上就是本文關(guān)于Java編程實(shí)現(xiàn)springMVC簡(jiǎn)單登錄實(shí)例的全部?jī)?nèi)容想,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:

Spring MVC實(shí)現(xiàn)的登錄攔截器代碼分享

SpringMVC攔截器實(shí)現(xiàn)監(jiān)聽session是否過期詳解

SpringMVC開發(fā)restful API之用戶查詢代碼詳解

如有不足之處,歡迎留言指出。

新聞名稱:Java編程實(shí)現(xiàn)springMVC簡(jiǎn)單登錄實(shí)例
文章URL:http://muchs.cn/article28/ispscp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化網(wǎng)站制作、網(wǎng)站設(shè)計(jì)公司、虛擬主機(jī)搜索引擎優(yōu)化、用戶體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司