概述
工程目录结构:
导入jar:
controllers.LoginController.java
package controllers;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import domain.*;
import model.Login;
@Controller
@RequestMapping("/user")
public class LoginController {
@RequestMapping(value = "/login", method = RequestMethod.POST)
public ModelAndView checkUser(User u) {
Login login = new Login();
ModelAndView mv = new ModelAndView();
if (login.verify(u.getUsername(), u.getPwd(), u.getCla().getClassName())) {
mv.setViewName("loginSuccess");
mv.addObject("username", u.getUsername());
} else
mv.setViewName("loginFailed");
return mv;
}
}
domain.Classes.java
package domain;
public class Classes {
private String className;
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
domain.User.java
package domain;
import domain.Classes;
public class User {
private String username;
private String pwd;
private Classes cla;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Classes getCla() {
return cla;
}
public void setCla(Classes cla) {
this.cla = cla;
}
}
model.Login.java
package model;
public class Login {
public boolean verify(String username, String password, String classname) {
if (username.equals("zlm") && password.equals("123") && classname.equals("jk1702"))
return true;
else
return false;
}
}
spring-mvc.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: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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<!-- 开启Controller注解支持 配置自定扫描的包 -->
<context:component-scan base-package="controllers" />
<!-- 配置视图解析器: 返回值解析为实际的物理视图 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
jsp.loginFailed.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Failed</title>
</head>
<body>
<h3>Username or password or class error!</h3>
</body>
</html>
jsp.loginSuccess.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Success</title>
</head>
<body>
<h3>Congratulations!</h3>
<p>User ${requestScope.username} login Success!</p>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Spring-MVC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Page</title>
</head>
<body>
<form action="user/login" method="post">
Username:<input type="text" name="username" /><br/>
Password:<input type="password" name="pwd" /> <br/>
Class:<input type="text" name="cla.className" /><br/>
<input type="submit" value="Login" />
</form>
</body>
</html>
最后
以上就是精明香氛为你收集整理的Spring MVC登录实例的全部内容,希望文章能够帮你解决Spring MVC登录实例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复