package hello;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
public final class HelloAction extends Action {
/**
* Process the specified HTTP request, and create the corresponding HTTP
* response (or forward to another web component that will create it).
* Return an <code>ActionForward</code> instance describing where and how
* control should be forwarded, or <code>null</code> if the response has
* already been completed.
*/
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
// These "messages" come from the ApplicationResources.properties file
MessageResources messages = getResources(request);
/*
* Validate the request parameters specified by the user
* Note: Basic field validation done in HelloForm.java
* Business logic validation done in HelloAction.java
*/
ActionMessages errors = new ActionMessages();
String userName = (String)((HelloForm) form).getUserName();
String badUserName = "Monster";
if (userName.equalsIgnoreCase(badUserName)) {
errors.add("username", new ActionMessage("hello.dont.talk.to.monster", badUserName ));
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
}
/*
* Having received and validated the data submitted
* from the View, we now update the model
*/
PersonBean pb = new PersonBean();
pb.setUserName(userName);
pb.saveToPersistentStore();
/*
* If there was a choice of View components that depended on the model
* (or some other) status, we'd make the decision here as to which
* to display. In this case, there is only one View component.
*
* We pass data to the View components by setting them as attributes
* in the page, request, session or servlet context. In this case, the
* most appropriate scoping is the "request" context since the data
* will not be neaded after the View is generated.
*
* Constants.PERSON_KEY provides a key accessible by both the
* Controller component (i.e. this class) and the View component
* (i.e. the jsp file we forward to).
*/
request.setAttribute( Constants.PERSON_KEY, pb);
// Remove the Form Bean - don't need to carry values forward
request.removeAttribute(mapping.getAttribute());
// Forward control to the specified success URI
return (mapping.findForward("SayHello"));
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
最简单的struts实例
共38个文件
jar:8个
tld:5个
class:4个
3星 · 超过75%的资源 需积分: 10 19 下载量 175 浏览量
2009-01-08
15:54:27
上传
评论
收藏 1.6MB RAR 举报
温馨提示
这个是本人测试通过的实例,希望对学习struts的新人有帮助。
资源推荐
资源详情
资源评论
收起资源包目录
helloapp.rar (38个子文件)
helloapp
.project 1KB
.mymetadata 297B
WebRoot
WEB-INF
struts-tiles.tld 8KB
.struts-config.mex 779B
web.xml 1KB
struts-config.xml 1KB
struts-bean.tld 9KB
struts-nested.tld 70KB
struts-html.tld 72KB
struts-logic.tld 14KB
validator-rules.xml 13KB
lib
commons-logging.jar 39KB
commons-digester.jar 178KB
commons-fileupload.jar 22KB
commons-beanutils.jar 148KB
struts.jar 506KB
commons-validator.jar 79KB
jakarta-oro.jar 64KB
commons-collections.jar 841KB
classes
hello
HelloForm.class 1KB
Constants.class 346B
PersonBean.class 623B
HelloAction.class 2KB
application.properties 555B
ApplicationResources.properties 68B
index.jsp 834B
META-INF
MANIFEST.MF 39B
struts-power.gif 2KB
Thumbs.db 6KB
404.jsp 845B
hello.jsp 1KB
.myeclipse
src
hello
HelloForm.java 1KB
HelloAction.java 3KB
Constants.java 220B
PersonBean.java 849B
ApplicationResources.properties 68B
.mystrutsdata 240B
.classpath 1KB
共 38 条
- 1
资源评论
- libaoan19712012-10-27很好的的实例,谢谢了。
zhengzhihong1985
- 粉丝: 0
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功