Enable Simple login Verification
Loginservlet.java
Import Javax.servlet.http.HttpServletRequest; Import Javax.servlet.http.HttpServletResponse; Import Org.springframework.web.servlet.ModelAndView; Import Org.springframework.web.servlet.mvc.AbstractController; public class Loginservlet extends abstractcontroller{ @Override Protected Modelandview handlerequestinternal (HttpServletRequest req, HttpServletResponse res) throws Exception { String message= ""; String name =req.getparameter ("name"); String Password = req.getparameter ("password"); SYSTEM.OUT.PRINTLN (name); SYSTEM.OUT.PRINTLN (password); if ("admin". Equals (name) && "123". Equals (password)) { Message= "Landing Success"; }else{ Message= "Landing not successful"; } Create a Modelandview object Modelandview Mav = new Modelandview ("index"); Save Message Mav.addobject ("message", message); return MAV; } } |
Xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="Http://java.sun.com/xml/ns/javaee" xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <display-name>SpringMVC</display-name> <!--Configuring the front-end controller- <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!--Configure Filters-- <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>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app> |
Note: The name of the Xx-servlet.xml is the same as the first-segment controller configured in Web. xml
Loginservlet-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- URL and Controller associated-- <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" xmlns:aop="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx="Http://www.springframework.org/schema/tx" xsi:schemalocation="Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-3.1.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-3.1.xsd Http://www.springframework.org/schema/mvc Http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd Http://www.springframework.org/schema/aop Http://www.springframework.org/schema/aop/spring-aop-3.1.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd "> <!--Configure the processor-- <bean id="Login" name="/login.do" class= "com.servlet.LoginServlet" ></bean> </beans> |
The above loginservlet-servlet.xml is configured with a processor and the page request action=login.do can be accessed.
The configuration to add the view resolver is as follows
<!--configuration View Resolver--
<bean class="Org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--prefixes--
<property name="prefix" value="/jsp/"></property>
<!--suffix--
<property name="suffix" value=". jsp"></property>
</bean>
Page:
<form action="login.do"> <input type="text" name="name"/> <br/> <input type="text" name="password"/> <br/> <input type="Submit" value=" login "> </form> |
Use inheritance Abstractcontroller only one processor can be written in a class
Use annotations you can have multiple processors in a class
Registering annotations Configuring normal classes (control inversion-mapping to objects)
Defining the processor
@Controller
Set the mapping name
@RequsetMapping (value= "/login.do") @RequsetMapping ("/login.do")
Package Com.servlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import Org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import Org.springframework.web.servlet.ModelAndView; /** * Notes * @author Administrator * */ @Controller @RequestMapping ("oo") Public class Annotionloginservlet { @RequestMapping ("/login.do") protected Modelandview Login (HttpServletRequest req, HttpServletResponse Res) throws Exception { Declaring message variables String message= ""; Get page input String name =req.getparameter ("name"); String Password = req.getparameter ("password"); Whether the comparison values are the same if ("admin". Equals (name) && "123". Equals (password)) { Message= "Landing Success"; System. out. println ("message:" +message); }Else{ Message= "Landing not successful"; System. out. println ("message:" +message); } Modelandview returned page/jsp/index.jsp Modelandview Mav = new modelandview ("index"); Mav.addobject ("message", message); return Mav; } } |
Page display
<form action="oo/login.do"> <input type="text" name="name"/> <br/> <input type="text" name="password"/> <br/> <input type="Submit" value=" login "> </form> |
Registering annotations in Loginservlet-servlet.xml driver and configuration generic class for processor
<!--load Annotation driver
<mvc:annotation-driven/>
<!--configuration Normal class is processor--
<bean id="annotation" class="Com.servlet.AnnotionLoginServlet"></bean>
SPRINGMVC Automatic Scanning device
Get the processor automatically
<!--component Scanners--
Base-package: Identify the processor under this package
<context:component-scan base-package= "Com.servlet" ></context:component-scan>
<?xml version="1.0" encoding="UTF-8"?> <!-- URL and Controller associated-- <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" xmlns:aop="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx="Http://www.springframework.org/schema/tx" xsi:schemalocation="Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-3.1.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-3.1.xsd Http://www.springframework.org/schema/mvc Http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd Http://www.springframework.org/schema/aop Http://www.springframework.org/schema/aop/spring-aop-3.1.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd "> <!-the first method of configuring the processor-- <bean id="Login" name="/login.do" class= "com.servlet.LoginServlet" ></bean> <!--configuration View Resolver-- <bean class="Org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--prefixes-- <property name="prefix" value="/jsp/"></property> <!--suffix-- <property name="suffix" value=". jsp"></property> </bean> <!—-the second way of configuring the processor-- <!--load Annotation driver <mvc:annotation-driven/> <!--configuration Normal class is processor-- <bean id="annotation" class="Com.servlet.AnnotionLoginServlet"></bean> <!--the third way of configuring the processor-- <!--component Scanners-- <!--context:component-scan base-package= "Com.servlet" ></context:component-scan>- </beans> |
Exception: No recognized processor
No mapping found for HTTP request with URI [/springmvc/oo/login.do] in Dispatcherservlet with name ' Loginservlet '
Spring MVC Small Case