Spring Framework循序浅进(2)-原创

80酷酷网    80kuku.com

  原创
作者:王斌 2005-04-06

通过第一个例子,我们对Spring Framework有了一点感性上的认识,现在我们通过一个具体的login例子,完整的阐述Spring Framework的实际应用!本文中的IDE为Eclipse!

1.我们使用的WEB服务器是tomcat,所以在D:\tomcat\webapps\webapps下新建一个目录spring,并在下面新建两个文件,用于Eclipse导入本工程.

.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 kind="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.在spring下新建一个目录WEB-INF,在下面新建文件web.xml,Config.xml以便tomcat初始化spring

web.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>  </property>  <property name="success_view">   <value>main</value>  </property> </bean></beans>




3.在web-inf下新建三个目录src,classes,lib,如图



4.在Eclipse中导入spring





ok,spring导入完成!!!

5.在spring目录下建立三个jsp文件

index.jsp


 <% page contentType="text/html; charset=gb2312" %><% page language="java" %><!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en"><html><head><title>Lomboz JSP</title></head><body bgcolor="#FFFFFF">

<form method="POST" action="login.do"><p align="left">登录</p>
用户名:<input type="text" name="username" >
密 码 :<input type="password" name="password" >
<p><input type="submit" value="提交" name="B1"><input type="reset" value="重置" name="B2"></p></form>

</body></html>




main.jsp


 <% page contentType="text/html; charset=gb2312" %><html><body><p>Login Success!!!</p>

</body></html>


loginfail.jsp


 <% page contentType="text/html; charset=gb2312" %><html><body><p>Login failed!!!</p>

</body></html>


6.在WEB-INF rc下新建包action,在action包下新建二个java文件,注意,要在项目中导入e: pring\dist下的所有jar包,否则Eclipse提示找不到类文件!

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(loginInfo.getPassword())) {//假设用户名是"qqq",密码"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.在WEB-INF\lib下拷入e: pring\dist下的所有jar包,注意一点,还要从sun的网站上下载jstl.jar,要不然会出现

"java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/fmt/LocalizationContext"的提示!全图如下(包括第一个例子):



ok!现在可以启动tomcat,IE上输入http://localhost/spring/index.jsp



验证失败!



验证成功!






分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: