SpringMVC登录实例

最近在学习SpringMVC,由于是自学在学习的过程中出现了很多问题,现在终于运行成功就分享出来:

在Eclipse创建wbe工程:首先进行Spring包的导入,我这里用的是Spring4.0,随后也会把Jar上传上来,因为我在运行的过程中如果Jar导入的不准确或者位置不对都会出现

java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoa这样的问题,我经过的好长时间把相关Jar整理下来,并正确导入就不会出现这样的问题了;

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>SpringMVC-01</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>
  <!-- 引入监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml</param-value>
  </context-param>  
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring-mvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping> 
</web-app>
spring-mvc.xml文件的设置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <!--Sping 会扫描com包下的所有包或者文件-->
<context:component-scan base-package="com" />
<!-- 引入视图解析器-->
  <bean 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
</beans>

Controller控制器类:
package com;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class SpringMVCController {
@Resource
SpringMVCService service;
@Resource
HttpServletRequest request;
@RequestMapping("hello")
public ModelAndView tologin(){
System.out.println("---------....");
return new ModelAndView("view/hello.jsp");
}
@RequestMapping("login")
public ModelAndView dologin(){
String username=request.getParameter("username");
System.out.println(username);
String password=request.getParameter("password");
String loginurl="view/hello.jsp";
return service.doLogin(username,password,loginurl);
}
}

Service服务层代码:
package com;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
@Service
public class SpringMVCService {
public ModelAndView doLogin(String username, String password,String loginurl) {
if(username==null||username.equals("")){
return new ModelAndView(loginurl,"error","用户名为空");
}
if(password.equals("")||password==null){
return new ModelAndView(loginurl,"error","密码不能为空");
}
if(password.equals("123456")&&username.equals("admin")){
return new ModelAndView("view/success.jsp","username",username);
}
return new ModelAndView(loginurl,"error","用户名和密码不正确");
}
}

Jsp页面:
hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>用户登录</h1>
<form action="login.do" method="post">
username:<input type="text" name="username">
<br>
password:<input type="password" name="password">
<input type="submit" value="提交">
</form>
<font color="red">${error}</font>
</body>
</html>

success.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Success</h1>
<br>
你好:<font>${username}</font>
</body>
</html>

控制层和服务层实在com包下面,jsp在view包下:


运行效果:

在浏览器中输入localhost:8080/SpringMVC-01/hello.do









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值