Spring Framework Progressive Shallow (2)-Original

Source: Internet
Author: User
Tags definition config reset xmlns tomcat
Original
Author: Wang Bin 2005-04-06

With the first example, we have a little perceptual understanding of the spring framework, and now we have a complete description of the actual application of the spring framework through a specific login example! The IDE in this article is eclipse!

1. The Web server we use is Tomcat, so create a new catalog spring under D:\tomcat\webapps\webapps, and create a new two files below for Eclipse to import this project.

. classpath

<?xml version= "1.0" encoding= "UTF-8"?><classpath> <classpathentry kind= "src" path= "WEB-INF/src"/> <classpathentry kind= "Con" path= "Org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry "Output" Path= "Web-inf/classes"/></classpath>

. Project

<?xml version= "1.0" encoding= "UTF-8"?><projectdescription> <name>spring1</name> < comment></comment> <projects> </projects> <buildSpec> <buildCommand> <name> org.eclipse.jdt.core.javabuilder</name> <arguments> </arguments> </buildCommand> </ buildspec> <natures> <nature>org.eclipse.jdt.core.javanature</nature> </natures></ Projectdescription>

2. Under Spring, create a new directory Web-inf, and create a new file Web.xml,config.xml below so that Tomcat initializes spring

Xml

 <?xml version= "1.0" encoding= "iso-8859-1" ><web-app 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 " version=" 2.4 ">  <servlet>   <servlet-name>Dispatcher</servlet-name>    <servlet-class>          Org.springframework.web.servlet.DispatcherServlet       </ Servlet-class>   <init-param>    <param-name>contextconfiglocation</ Param-name>       <param-value>/WEB-INF/Config.xml</param-value>    </init-param>  </servlet>   <servlet-mapping>  < Servlet-name>dispatcher</servlet-name>   <url-pattern>*.do</url-pattern>   </sErvlet-mapping></web-app>



Config.xml


 <?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE beans Public "-//spring//dtd bean//en"   "HTTP://WWW.SPRINGFRAMEWORK.ORG/DTD/SPRING-BEANS.DTD" >< beans> <!--Definition of View Resolver--> <bean id= "Viewresolver" class= " Org.springframework.web.servlet.view.InternalResourceViewResolver ">  <property name=" Viewclass " > <value>org.springframework.web.servlet.view.jstlview </value> </property>  < Property name= "prefix" > <value>/</value> </property>  <property name= "suffix" >  <value>.jsp</value> </property> </bean>  <!--Request Mapping --> <bean id= "urlmapping" class= "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >   <property name= "Mappings" >   <props>    <prop key= "/login.do" >loginaction</prop>   </pRops>  </property> </bean>

<!---action definition--> <bean id= "loginaction" class= "action. Loginaction "> <property name=" Commandclass "> <value>action. logininfo</value> </property> <property name= "Fail_view" > <value>loginfail</value> & lt;/property> <property name= "Success_view" > <value>main</value> </property> </bean ></beans>




3. Create a new three directory src,classes,lib under Web-inf, as shown in



4. Import Spring in Eclipse





Ok,spring Import Complete!!!

5. Create three JSP files under the spring directory

index.jsp


<%@ page contenttype= "text/html; charset=gb2312 "%><%@ page language=" java "%><! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<form method= "POST" action= "login.do" ><p align= "left" > Login </p><br> Username: <input type= "Text" Name= "username" ><br> Password: <input type= "password" name= "password" ><br><p><input type= " Submit "value=" Name= "B1" ><input type= "reset" value= "reset" name= "B2" ></p></form>

</body>



main.jsp


<%@ page contenttype= "text/html; charset=gb2312 "%>
</body>

loginfail.jsp


<%@ page contenttype= "text/html; charset=gb2312 "%>
</body>

6. Under the Web-inf RC New package action, under the action package to create a new two Java files, note that in the project to import all the jar packages under E:pring\dist, otherwise the eclipse hint can not find the class file!

Logininfo.java


Package action;

public class Logininfo {private string username, private string password, public string GetPassword () {return password;  public void SetPassword (string password) {This.password = password;} public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;}}




Loginaction.java


Package Action;import Java.util.*;import Org.springframework.validation.*;import org.springframework.web.servlet.* ; Import org.springframework.web.servlet.mvc.*;

public class Loginaction extends Simpleformcontroller {private String fail_view;

Private String Success_view;

Protected Modelandview onSubmit (Object cmd, bindexception ex) throws Exception {Logininfo Logininfo = (logininfo) cmd   ;   if (login (logininfo) = = 0) {HashMap result_map = new HashMap ();   Result_map.put ("Logininfo", logininfo);   return new Modelandview (This.getsuccess_view (), result_map);  else {return new Modelandview (This.getfail_view ()); } }

private int Login (Logininfo logininfo) {if ("QQQ". Equalsignorecase (Logininfo.getusername ()) && "1". Equals (log  Ininfo.getpassword ()) {//assuming user name is "QQQ", password "1" return 0; return 1; }

Public String Getfail_view () {return fail_view;}

Public String Getsuccess_view () {return success_view;}

public void Setfail_view (string string) {Fail_view = string;}

public void Setsuccess_view (string string) {Success_view = string;}}




7. In the web-inf\lib under the e:pring\dist all jar packs, note that, but also from the Sun's Web site download Jstl.jar, or will appear

"Java.lang.noclassdeffounderror:javax/servlet/jsp/jstl/fmt/localizationcontext" Tips! The full picture is as follows (including the first example):



Ok! can now start Tomcat,ie input http://localhost/spring/index.jsp



Validation failed!



Verify success!






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.