Spring MVC 入门 ,从简单登陆做起

本文详细介绍了如何使用Spring框架实现一个简单的登录系统,包括配置web.xml、映射文件、控制器类、登录页面和成功页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步在 web.xml中配置如下:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"

           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

           xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

           http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

           <!-- 加载Spring配置文件 -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/applicationContext.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!-- 对字符集进行过滤 -->

<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>

</filter>

<filter-mapping>

<filter-name>encodingFilter</filter-name>

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

</filter-mapping>

<!-- 配置spring的拦截器 -->

  <servlet>

  <servlet-name>login</servlet-name>

  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

  <load-on-startup>1</load-on-startup>

  </servlet>

  <servlet-mapping>

  <servlet-name>login</servlet-name>

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

  </servlet-mapping>

</web-app>

第二部 配置映射文件,此映射文件的文件名为web.xml中配置的DispatcherServlet的名字-servlet.xml,我的映射文件为login-servlet.xml,此配置文件将会被web容器自动加载,内容如下:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 

"http://www.springframework.org/dtd/spring-beans.dtd"> 

 <beans>

      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

          <property name="prefix" value="/" />

          <property name="suffix" value=".jsp" />

      </bean>

      

 <bean id="LoginController" class="com.controller.LoginController"></bean>

 

     <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

         <property name="mappings">

             <props>

                 <prop key="/login.do">LoginController</prop>

             </props>

         </property>

     </bean>

 </beans>

其中 prefix表示存放jsp文件的位置,suffix指定为jsp文件

第三步  进入Controller 

public class LoginController implements Controller{

public ModelAndView handleRequest(HttpServletRequest request,

HttpServletResponse response) throws Exception {

String userName = request.getParameter("userName");

String userPass = request.getParameter("userPass");

if("devin".equals(userName)&&"jintaiyang".equals(userPass)){

request.setAttribute("userName", userName);

return new ModelAndView("success");

}

else{

request.setAttribute("error","用户名和密码错误");

return new ModelAndView("login");

}

}

}

上面的success和login都是逻辑名,只需保证success.jsp和login.jsp存在就行

第四步  login.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>
<form action="login.do" method="post">
userName:<input type="text" name="userName"><br>
userPass:<input type="password" name="userPass"><br>
<input type="submit" value="登陆"><input type="reset" value="reset" value="重置"><br>
<font color="red">${error }</font>
</form>
</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>
登陆成功,欢迎你:${userName }
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sust2012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值