1.开发环境
开发之前首先应该准备好开发环境,Eclipse 3.X是一个不错的Java IDE,有很多插件可以供我们选择,服务器用Tomcat就行了。安装好Eclipse和Tomcat之后,还需要两个插件:tomcat 插件和Struts插件,以下是相关下载地址:
Eclipse SDK:
http://www.eclipse.org/downloads/index.php
Struts:
https://sourceforge.jp/projects/amateras/files/ StrutsIDE+htmlEditor,htmlEditer也是必要的
Tomcat :
http://www.sysdeo.com/eclipse/tomcatplugin
Plugin Search:
http://eclipse-plugins.2y.net/eclipse/search.jsp
插件的安装和配置有问题请直接Google。
2.开始
入门嘛,我们就找一个最简单的Login就可以了
新建Tomcat project,如下图
加入struts支持
新建一个FormBean:LoginForm
/**
*
*/
package com.test;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
/**
* @author lzy
*
*/
public class LoginForm extends ActionForm {
private String name=null;
private String password=null;
/* (non-Javadoc)
* @see org.apache.struts.action.ActionForm#reset(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
*/
@Override
public void reset(ActionMapping arg0, HttpServletRequest arg1) {
// TODO Auto-generated method stub
name=null;
password=null;
}
/* (non-Javadoc)
* @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
*/
@Override
public ActionErrors validate(ActionMapping arg0, HttpServletRequest arg1) {
// TODO Auto-generated method stub
ActionErrors errors=new ActionErrors();
if(name==null||name.length()<1){
errors.add("username",new ActionMessage("errors.login.name.empty"));
}
if(password==null||password.length()<1){
errors.add("password",new ActionMessage("errors.login.password.empty"));
}
return errors;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
/**
* @return Returns the password.
*/
public String getPassword() {
return password;
}
/**
* @param password The password to set.
*/
public void setPassword(String password) {
this.password = password;
}
}
新建一个Action:LoginAction
/**
*
*/
package com.test;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
/**
* @author lzy
*
*/
public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
LoginForm loginForm=(LoginForm)actionForm;
String name=loginForm.getName();
String password=loginForm.getPassword();
if(name.equals("name")&&password.equals("password")){
HttpSession session=request.getSession(true);
session.setAttribute("LOGGED_USER",name);
return mapping.findForward("success");
}
else{
ActionMessages errors=new ActionMessages();
errors.add("login.failure",new ActionMessage("errors.login.failure"));
this.saveErrors(request,errors);
return mapping.findForward("failure");
}
}
}
新建两个JSP页面,login.jsp,index.jsp
login.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-tiles" prefix="tiles" %>
<html:html>
<head>
<html:base />
</head>
<body>
<center>
<html:errors />
<html:form action="/login.do" method="POST">
<table border="1">
<tr>
<td colspan="2"><bean:message key="login.jsp.form.title"/></td>
</tr>
<tr>
<td align="right"><bean:message key="login.jsp.name"/></td>
<td><html:text property="name" size="20"></html:text></td>
</tr>
<tr>
<td align="right"><bean:message key="login.jsp.password"/></td>
<td><html:password property="password" size="20"></html:password></td>
</tr>
<tr>
<td colspan="2">
<html:submit><bean:message key="form.submit"/></html:submit>
<html:reset><bean:message key="form.reset"/></html:reset>
</td>
</tr>
</table>
</html:form>
</center>
</body>
</html:html>
index.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-nested" prefix="nested" %>
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
<title></title>
</head>
<body>
hello
<logic:present name="LOGGED_USER">,<bean:write name="LOGGED_USER"/>
</logic:present>
</body>
</html:html>
编辑WEB-INF/lib下的struts-config.xml
struts-config.xml
<?xml version="1.0"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources>
</data-sources>
<form-beans>
<form-bean name="loginForm" type="com.lzy.LoginForm"/>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
</global-forwards>
<action-mappings>
<action path="/login" name="loginForm" type="com.lzy.LoginAction" scope="session" validate="true" input="/login.jsp">
<forward name="failure" path="/login.jsp"/>
<forward name="success" path="/index.jsp"/>
</action>
</action-mappings>
<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<message-resources parameter="MessageResources"/>
</struts-config>
最后是资源文件
# -- standard errors --
errors.header=<UL><font color="red"><h2>Errors:</h2>
errors.prefix=<LI>
errors.suffix=</LI>
errors.footer=</font></UL><hr>
form.submit=Submit
form.reset=Reset
# -- login.jsp --
login.jsp.title=Login
login.jsp.form.title=Login
login.jsp.name=Name:
login.jsp.password=Password:
# -- login errors --
errors.login.name.empty=The name can not be empty!
errors.login.password.empty=The password can not be empty!
errors.login.failure=Login failed!<br> Please check your username and password!
3.测试
在test工程中选择tomcat project->Update context definition
然后运行Tomcat
http://127.0.0.1:8080/test/login.jsp
这几天在图书馆看到了一本书《Java SOAP编程指南》,出于对正兴起的web service的好奇,借了回来。看了一下SOAP的介绍,没太明白,只知道是Simple Object Access Protocol,和XML结合可以提供跨平台的服务。
编程的人总有这么一个爱好,开始的时候总要拿一个HelloWorld似的例子试试,我也不例外。 要试当然要有SOAP的运行环境。按照书上的指示,装好了Tomcat,下载了相关的包,将soap.jar ,mail.jar,activatio.jar,xercesImpl.jar 加入到了环境变量classpath中,将soap.war拷贝到了TOMCAT的webapps目录下。
运行SOAP的测试目录:HTTP://127.0.0.1:8080/soap/发现soap已经正常工作。 接着便是部署新的SOAP服务了,按照书上的源码,写好了测试用的部署类和客户端类,编译通过。在部署新的SOAP的web页面下按照书上的步骤添好表单并部署完成。将测试所在目录添加到classpath中。 重启tomcat。 运行刚才编译好的客户端类,结果NoClassDefError。
试了好几遍,都是这样,很是让我郁闷啊。 看了另一本书《java web服务应用开发详解》终于找到了答案: 运行客户端测试程序时,***Client.class是在Package test中的 运行时退回到test的上级目录,java test.***Client 即可。 我想遇到类似问题,还是对java的包不熟悉。