Oops! Spring Web Quick Start! (eclipse europa + tomcat 5.5+spring 2.06+lomboz S3.3RC1)
Oops! Spring Web Quick Start!
(eclipse europa + tomcat 5.5+spring 2.06+lomboz S3.3RC1)
前期准备:
eclipse-java-europa-win32.zip
apache-tomcat- 5.5.23 .exe
tomcatPluginV31.zip
spring-framework- 2.0.6 -with-dependencies.zip
org.objectweb.lomboz-and-prereqs-S-3.3RC1-200708181505.zip
Reference:
http://www.blogjava.net/pixysoft/archive/2007/08/29/141048.html
Quick Start
新建一个动态网页项目:名称为ShitSpring
得到的结构
在WebContent/WEB-INF/lib目录下面导入以下包。这些包全部可以在spring的zip内找到。
在WEB-INF目录下面新建一个目录tlds,导入以下文件。可以在eclipse目录下面搜索找到这2个文件。
修改WEB-INF目录下面的web.xml文件为:
<?
xml version="1.0" encoding="UTF-8"
?>
< web-app id ="WebApp_ID" 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" >
< display-name >
springmvc </ display-name >
< 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 >
< jsp-config >
< taglib >
< taglib-uri > http://java.sun.com/jstl/core_rt </ taglib-uri >
< taglib-location > /WEB-INF/tlds/c.tld </ taglib-location >
</ taglib >
</ jsp-config >
</ web-app >
< web-app id ="WebApp_ID" 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" >
< display-name >
springmvc </ display-name >
< 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 >
< jsp-config >
< taglib >
< taglib-uri > http://java.sun.com/jstl/core_rt </ taglib-uri >
< taglib-location > /WEB-INF/tlds/c.tld </ taglib-location >
</ taglib >
</ jsp-config >
</ web-app >
在WEB-INF目录下面增加一个Config.xml,内容为:
<?
xml version="1.0" encoding="UTF-8"
?>
< web-app id ="WebApp_ID" 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" >
< display-name >
springmvc </ display-name >
< 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 >
< jsp-config >
< taglib >
< taglib-uri > http://java.sun.com/jstl/core_rt </ taglib-uri >
< taglib-location > /WEB-INF/tlds/c.tld </ taglib-location >
</ taglib >
</ jsp-config >
</ web-app >
< web-app id ="WebApp_ID" 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" >
< display-name >
springmvc </ display-name >
< 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 >
< jsp-config >
< taglib >
< taglib-uri > http://java.sun.com/jstl/core_rt </ taglib-uri >
< taglib-location > /WEB-INF/tlds/c.tld </ taglib-location >
</ taglib >
</ jsp-config >
</ web-app >
在WEB-INF目录下面增加一个文件index.html
<!
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"
>
< html >
< head >
< title > Insert title here </ title >
</ head >
< body >
< form method ="post" action ="login.do" >
< p align ="center" > 登录 </ p >
< br > 用户名:
< input type ="text" name ="username" >
< br >
密码:
< input type ="password" name ="password" >
< br >
< p >
< input type ="submit" value ="提交" name ="B1" >
< input type ="reset" value ="重置" name ="B2" >
</ p >
</ form >
</ body >
</ html >
< html >
< head >
< title > Insert title here </ title >
</ head >
< body >
< form method ="post" action ="login.do" >
< p align ="center" > 登录 </ p >
< br > 用户名:
< input type ="text" name ="username" >
< br >
密码:
< input type ="password" name ="password" >
< br >
< p >
< input type ="submit" value ="提交" name ="B1" >
< input type ="reset" value ="重置" name ="B2" >
</ p >
</ form >
</ body >
</ html >
在WEB-INF目录下面增加一个目录为view,在view目录下增加一个文件main.jsp
<%
@ page language
=
"
java
"
contentType
=
"
text/html; charset=ISO-8859-1
"
pageEncoding = " ISO-8859-1 " %>
<% @taglib prefix = " c " uri = " http://java.sun.com/jstl/core_rt " %>
< html >
< head >
</ head >
< body >
< p > Shit! Spring! </ p >
< p > Current User: < c:out value ="${logininfo.username}" />< br >
</ p >
< p > Your Current messages: </ p >
< c:forEach items ="${messages}" var ="item" begin ="0" end ="9" step ="1"
varStatus ="var" >
< c:if test ="${var.index%2==0}" >
*
</ c:if >
${item} < br >
</ c:forEach >
</ body >
</ html >
pageEncoding = " ISO-8859-1 " %>
<% @taglib prefix = " c " uri = " http://java.sun.com/jstl/core_rt " %>
< html >
< head >
</ head >
< body >
< p > Shit! Spring! </ p >
< p > Current User: < c:out value ="${logininfo.username}" />< br >
</ p >
< p > Your Current messages: </ p >
< c:forEach items ="${messages}" var ="item" begin ="0" end ="9" step ="1"
varStatus ="var" >
< c:if test ="${var.index%2==0}" >
*
</ c:if >
${item} < br >
</ c:forEach >
</ body >
</ html >
在view目录下面增加一个文件loginfail.jsp
<%
@ page language
=
"
java
"
contentType
=
"
text/html; charset=ISO-8859-1
"
pageEncoding = " ISO-8859-1 " %>
<! 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=ISO-8859-1" >
< title > Insert title here </ title >
</ head >
< body >
Login Fail!
</ body >
</ html >
pageEncoding = " ISO-8859-1 " %>
<! 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=ISO-8859-1" >
< title > Insert title here </ title >
</ head >
< body >
Login Fail!
</ body >
</ html >
在项目的src目录下面增加2个类:LoginAction.java / LoginInfo.java
LoginAction.java
package
net.oscar.action;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
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);
List msgList = new LinkedList();
msgList.add( " Shit " );
msgList.add( " Spring " );
msgList.add( " Quick Start! " );
result_map.put( " messages " , msgList);
return new ModelAndView( this .getSuccess_view(), result_map);
} else
{
return new ModelAndView( this .getFail_view());
}
}
private int login(LoginInfo loginInfo)
{
if ( " shit " .equalsIgnoreCase(loginInfo.getUsername())
&& " shit " .equals(loginInfo.getPassword()))
{
return 0 ;
}
return 1 ;
}
public String getFail_view()
{
return fail_view;
}
public void setFail_view(String fail_view)
{
this .fail_view = fail_view;
}
public String getSuccess_view()
{
return success_view;
}
public void setSuccess_view(String success_view)
{
this .success_view = success_view;
}
}
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
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);
List msgList = new LinkedList();
msgList.add( " Shit " );
msgList.add( " Spring " );
msgList.add( " Quick Start! " );
result_map.put( " messages " , msgList);
return new ModelAndView( this .getSuccess_view(), result_map);
} else
{
return new ModelAndView( this .getFail_view());
}
}
private int login(LoginInfo loginInfo)
{
if ( " shit " .equalsIgnoreCase(loginInfo.getUsername())
&& " shit " .equals(loginInfo.getPassword()))
{
return 0 ;
}
return 1 ;
}
public String getFail_view()
{
return fail_view;
}
public void setFail_view(String fail_view)
{
this .fail_view = fail_view;
}
public String getSuccess_view()
{
return success_view;
}
public void setSuccess_view(String success_view)
{
this .success_view = success_view;
}
}
LoginInfo.java
package
net.oscar.action;
public class LoginInfo
{
private String username;
private String password;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this .username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this .password = password;
}
}
public class LoginInfo
{
private String username;
private String password;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this .username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this .password = password;
}
}
最后整个项目目录变为:
鼠标右键点击项目,选择Run As / Run on Server
选择tomcat服务器
输入shit/shit。成功!