1. Create a Dynamic Web page project and modify the Web.xml.
Web.xml contents are as follows:
<?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 ">
<servlet>
<servlet-name>dispatch</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value> <!--servlet.xml path-->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatch</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2. Create servlet.xml files and modify their contents according to their own documents.
File contents:
<?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:mvc= "Http://www.springframework.org/schema/mvc"
xmlns:context= "Http://www.springframework.org/schema/context"
Xsi:schemalocation= "
Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
Http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd ">
<context:component-scan base-package= "Logintest" ></context:component-scan> <!-- The package name of the Java control class that corresponds to the action-->
<mvc:annotation-driven/>
<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name= "prefix" value= "/web-inf/jsp/"/> <!--processing requests and turning to file location-->
<property name= "suffix" value= ". jsp"/> <!--file suffix can also be ASP HTML-->
<property name= "Viewclass"
Value= "Org.springframework.web.servlet.view.JstlView"/>
</bean>
</beans>
3. Set up landing page
<form action= "login.do" method= "POST" >
<input type= "text" name = "username" ><p>
<input type= "password" name = "Password" ><p>
<input type= "Submit" value= "Submit" >
</form>
4. Importing jar packages is critical to importing jar packages. Most of the jar packages end with Release.jar, and all imports can be wrong. The selection of Jar package versions also needs to be considered with JDK and Tomcat versions. Jdk1.8 should consider spring4.0 and above.
5. Establish Control class
packagelogintest; corresponding to the package name in the Servlet.xml file
importOrg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
@Controller//represented as control class
Public class Logincontrol {
@RequestMapping ("Login")//corresponds to the contents of the form action
Public Stringlogin (String username,string password) {
if ("Test". Equals (username)) {
System. out. println (username + "Login successful");
return "Success";//Logical View name jump page defaults to forward
}
return "Error";
}
}
6. Establishment of SUCCESS.JSP and error.jsp
Don't repeat it.
7. Test results
Download source Code Link: http://download.csdn.net/detail/u011489887/9731973