一个简单完整的Struts应用示例

创建jsp页面

文件名 :helloWorld.jsp

< %@ page language = "java" pageEncoding = "gb2312" % >
< %@ taglib uri = "http://struts.apache.org/tags-bean" prefix = "bean" % > 
< %@ taglib uri = "http://struts.apache.org/tags-html" prefix = "html" % >
< %@ taglib uri = "http://struts.apache.org/tags-logic" prefix = "logic" % >
<html > 
     <head >
         <title > <bean :message key = "helloworld.title" / > < /title >
     < /head >
     <body style = "FONT-SIZE: 12px" >
         <html :form action = "/helloWorld" >
             <bean :message key = "helloworld.message" / > <br >
             <bean :message key = "helloworld.form.name" / >
             <html :text property = "name" / >
             <html :errors property = "name" / > <br / >
             <html :submit value = "确定" / >
             <html :reset value = "重置" / >
         < /html :form >
         <logic :notEmpty name = "user" >
            Hello <bean :write name = "user" property = "name" / >
           < ! -- <html :errors property = "name" / > -- >
         < /logic :notEmpty >
         <html :errors property = "name" / >
     < /body >
< /html >

创建ActionForm

文件名 :HelloWorldForm.java
package forms;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class HelloWorldForm extends ActionForm {
     /** name property */
     private String name;
     /** 
     * Method validate
     * @param mapping
     * @param request
     * @return ActionErrors
     */


    @SuppressWarnings( "deprecation")
     public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
         if(name.length() == 0)
        {
             //ActionError error = new ActionError("helloworld.name.required");
             //errors.add("name",error);
            ActionMessage message = new ActionMessage( "helloworld.name.required");
            errors.add( "name",message);
        }
         return errors;
    }
     /** 
     * Method reset
     * @param mapping
     * @param request
     */

     public void reset(ActionMapping mapping, HttpServletRequest request) {
         this.name = "";
    }
     /** 
     * Returns the name.
     * @return String
     */

     public String getName() {
         return name;
    }
     /** 
     * Set the name.
     * @param name The name to set
     */

     public void setName(String name) {
         this.name = name;
    }
}

 


 

 

创建Action

文件名 :HelloWorldAction.java
package actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 beans.User;
import forms.HelloWorldForm;
public class HelloWorldAction extends Action {
     /** 
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */

     public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        HelloWorldForm helloWorldForm = (HelloWorldForm) form;
        String name = helloWorldForm.getName();
        User user = new User();
        user.setName(name);
        request.setAttribute( "user", user);
         return mapping.findForward( "succ");
    }
}

 


 

 

创建javabean模型组件

文件名 :User.java
package beans;

public class User {
     private String name;

     public void doSomething()
    {
         //进行具体的业务逻辑处理
    }
     /**
     * @return the name
     */

     public String getName() {
         return name;
    }
     /**
     * @param name the name to set
     */

     public void setName(String name) {
         this.name = name;
    }

}



资源文件
文件名 :ApplicationResources.properties
# Resources for parameter 'com.jspwebdev.struts.ApplicationResources'
# Project chapt13

#page helloworld.jsp
helloworld.title =Struts/u7b80/u5355/u793a/u4f8b/u7a0b/u5e8f --HelloWorld
helloworld.message =/u8bf7/u8f93/u5165/u59d3/u540d
helloworld.form.name =/u59d3/u540d/uff1a
helloworld.name.required =/u59d3/u540d/u4e0d/u80fd/u4e3a/u7a7a/uff01

 


 

 

配置信息

struts -config.xml
< ?xml version = "1.0" encoding = "UTF-8" ? >
< !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 / >
   <form -beans >
     <form -bean name = "helloWorldForm" type = "forms.HelloWorldForm" / >
   < /form -beans >
   <global -exceptions / >
   <global -forwards / >
   <action -mappings >
     <action
      attribute = "helloWorldForm"
      input = "/helloWorld.jsp"
      name = "helloWorldForm"
      path = "/helloWorld"
      scope = "request"
      validate = "true"
      type = "actions.HelloWorldAction" >
       <forward name = "succ" path = "/helloWorld.jsp" / >
     < /action >
   < /action -mappings >
   <message -resources parameter = "resources.ApplicationResources" / >

< /struts -config >
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值