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等。
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é)果:


使用其他方式傳遞:
/**
* 返回的是一個(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之用戶查詢代碼詳解
如有不足之處,歡迎留言指出。
- 詳解springmvc控制登錄用戶session失效后跳轉(zhuǎn)登錄頁面
- SpringMVC攔截器實(shí)現(xiàn)登錄認(rèn)證
- Spring MVC+mybatis實(shí)現(xiàn)注冊(cè)登錄功能
- SpringMVC 實(shí)現(xiàn)用戶登錄實(shí)例代碼
- SpringMVC攔截器實(shí)現(xiàn)單點(diǎn)登錄
- springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能(上)
- springmvc下實(shí)現(xiàn)登錄驗(yàn)證碼功能示例
- Spring mvc 實(shí)現(xiàn)用戶登錄的方法(攔截器)
- spring mvc實(shí)現(xiàn)登錄賬號(hào)單瀏覽器登錄
- spring?MVC實(shí)現(xiàn)簡(jiǎn)單登錄功能
相關(guān)文章
CountDownLatch和Atomic原子操作類源碼解析
這篇文章主要為大家介紹了CountDownLatch和Atomic原子操作類的源碼解析以及理解應(yīng)用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
解決mybatis返回boolean值時(shí)數(shù)據(jù)庫返回null的問題
這篇文章主要介紹了解決mybatis返回boolean值時(shí)數(shù)據(jù)庫返回null的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Java OpenCV實(shí)現(xiàn)人臉識(shí)別過程詳解
這篇文章主要介紹了Java OpenCV實(shí)現(xiàn)人臉識(shí)別過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
關(guān)于Java項(xiàng)目讀取resources資源文件路徑的那點(diǎn)事
這篇文章主要介紹了關(guān)于Java項(xiàng)目讀取resources資源文件路徑的那點(diǎn)事,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07

