|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
有了这样一个呼声:让java代替C语言成为基本语言。这些足以说明java简单易学的这个优点。其次,java的功能强大,前面我也提到了,EJB3.0的推出使java成为了大型项目的首选。原创
王斌2005-04-06
经由过程第一个例子,我们对SpringFramework有了一点理性上的熟悉,如今我们经由过程一个详细的login例子,完全的论述SpringFramework的实践使用!本文中的IDE为Eclipse!
1.我们利用的WEB服务器是tomcat,以是在D: omcatwebappswebapps下新建一个目次spring,并鄙人面新建两个文件,用于Eclipse导进本工程.
.classpath
<?xmlversion="1.0"encoding="UTF-8"?><classpath><classpathentrykind="src"path="WEB-INF/src"/><classpathentrykind="con"path="org.eclipse.jdt.launching.JRE_CONTAINER"/><classpathentrykind="output"path="WEB-INF/classes"/></classpath>
.project
<?xmlversion="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
<?xmlversion="1.0"encoding="ISO-8859-1"?><web-appxmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://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
<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd"><beans><!--DefinitionofViewResolver--><beanid="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><propertyname="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property><propertyname="prefix"><value>/</value></property><propertyname="suffix"><value>.jsp</value></property></bean><!--RequestMapping--><beanid="urlMapping"class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><propertyname="mappings"><props><propkey="/login.do">LoginAction</prop></props></property></bean>
<!---ActionDefinition--><beanid="LoginAction"class="action.LoginAction"><propertyname="commandClass"><value>action.LoginInfo</value></property><propertyname="fail_view"><value>loginfail</value></property><propertyname="success_view"><value>main</value></property></bean></beans>
3.在web-inf下新建三个目次src,classes,lib,如图
4.在Eclipse中导进spring
ok,spring导进完成!!!
5.在spring目次下创建三个jsp文件
index.jsp
<%@pagecontentType="text/html;charset=gb2312"%><%@pagelanguage="java"%><!DOCTYPEHTMLPUBLIC"-//w3c//dtdhtml4.0transitional//en"><html><head><title>LombozJSP</title></head><bodybgcolor="#FFFFFF">
<formmethod="POST"action="login.do"><palign="left">登录</p><br>用户名:<inputtype="text"name="username"><br>密码:<inputtype="password"name="password"><br><p><inputtype="submit"value="提交"name="B1"><inputtype="reset"value="重置"name="B2"></p></form>
</body></html>
main.jsp
<%@pagecontentType="text/html;charset=gb2312"%><html><body><p>LoginSuccess!!!</p>
</body></html>
loginfail.jsp
<%@pagecontentType="text/html;charset=gb2312"%><html><body><p>Loginfailed!!!</p>
</body></html>
6.在WEB-INFrc下新建包action,在action包下新建二个java文件,注重,要在项目中导进e:pringdist下的一切jar包,不然Eclipse提醒找不到类文件!
LoginInfo.java
packageaction;
publicclassLoginInfo{privateStringusername;privateStringpassword;publicStringgetPassword(){returnpassword;}publicvoidsetPassword(Stringpassword){this.password=password;}publicStringgetUsername(){returnusername;}publicvoidsetUsername(Stringusername){this.username=username;}}
LoginAction.java
packageaction;importjava.util.*;importorg.springframework.validation.*;importorg.springframework.web.servlet.*;importorg.springframework.web.servlet.mvc.*;
publicclassLoginActionextendsSimpleFormController{privateStringfail_view;
privateStringsuccess_view;
protectedModelAndViewonSubmit(Objectcmd,BindExceptionex)throwsException{LoginInfologinInfo=(LoginInfo)cmd;if(login(loginInfo)==0){HashMapresult_map=newHashMap();result_map.put("logininfo",loginInfo);returnnewModelAndView(this.getSuccess_view(),result_map);}else{returnnewModelAndView(this.getFail_view());}}
privateintlogin(LoginInfologinInfo){if("qqq".equalsIgnoreCase(loginInfo.getUsername())&&"1".equals(loginInfo.getPassword())){//假定用户名是"qqq",暗码"1"return0;}return1;}
publicStringgetFail_view(){returnfail_view;}
publicStringgetSuccess_view(){returnsuccess_view;}
publicvoidsetFail_view(Stringstring){fail_view=string;}
publicvoidsetSuccess_view(Stringstring){success_view=string;}}
7.在WEB-INFlib下拷进e:pringdist下的一切jar包,注重一点,还要从sun的网站高低载jstl.jar,要否则会呈现
"java.lang.NoClassDefFoundError:javax/servlet/jsp/jstl/fmt/LocalizationContext"的提醒!全图以下(包含第一个例子):
ok!如今能够启动tomcat,IE上输出http://localhost/spring/index.jsp
考证失利!
考证乐成!
在性能方面,在windows平台下,.net可能是占强项,要是把.net放在sun开发的操作系统上去运行呢?根本就运行不了,.net对其它操作系统的支持也很弱,性能也可能比不上java。 |
|