击破struts 步步为营

本文通过一个简单的Struts项目演示了如何使用Struts框架进行web开发,包括配置Tomcat、创建ActionForm和Action类、使用JSP页面及Struts标签库等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于Tomcat + struts的环境我就不再叙述了,可以参考超简单配置tomcat + struts

建议看着篇文章之前先看看我另一片文章《手把手struts入门示例》,在本文,我不再作解析。

Helloword!
咱也俗一下,先来say 一下 hello。

package  hello;
import  org.apache.struts.action.ActionForm;

public   class  helloActionForm  extends  ActionForm {
    
private String yourName;

    
public void setYourName(String yourName){
        
this.yourName = yourName;
    }

    
public String getYourName(){
        
return yourName;
    }

}
helloActionForm.java

package  hello;

import  org.apache.struts.action. * ;
import  javax.servlet.http. * ;


public   class  helloAction  extends  Action {
    
    
public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response)
    
{
        helloActionForm actionForm 
= (helloActionForm)form;

        String yName 
= actionForm.getYourName() ;
        yName 
= yName + "A";
        actionForm.setYourName(yName);

        
return mapping.findForward("showName");
     }

}
helloAction.java

<% @ taglib uri="http://struts.apache.org/tags-html" prefix="html"  %>

< BR >
Enter Your Name:

< html:form  action  ="/sayhi"  focus ="yourName" >
    
< html:text  property ="yourName" />
    
< html:submit  property ="submit"  value ="SayHello"   />
</ html:form >
index.jsp

<% @ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"  %>

Your Name is 
< bean:write  name ="helloForm"  property ="yourName"  scope ="request" />

< jsp:include  page ="index.jsp"  flush ="true" />
result.jsp

编译:
javac -classpath "D:/.../apache-tomcat-5.5.12/common/lib/servlet-api.jar";"D:/.../struts-1.3.8/lib/struts.jar";. *.java

<? xml version = " 1.0 "  encoding = " ISO-8859-1 "   ?>

<! DOCTYPE struts - config PUBLIC
          
" -//Apache Software Foundation//DTD Struts Configuration 1.1//EN "
          
" http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd " >

< struts - config >

<!--   ========================================  Form Bean Definitions  -->

    
< form - beans >
      
< form - bean name = " helloForm "  type = " hello.helloActionForm " />
    
</ form - beans >

<!--   ===================================  Action Mapping Definitions  -->

    
< action - mappings >
      
< action path = " /sayhi "  type = " hello.helloAction "  name = " helloForm "
       scope
= " request "  validate = " false " >
          
< forward name = " showName "  path = " /result.jsp " />
      
</ action >
    
</ action - mappings >

</ struts - config >

struts-config.xml

配置到空的struts 项目中 struts-blank-1.*,并改名为hello.目录结构如下。
hello

│  index.jsp
│  result.jsp
│       
├─META-INF
│  │  MANIFEST.MF
│  │ 
│  └─maven
│      └─org.apache.struts
│          └─struts-blank
│                  pom.properties
│                  pom.xml
│                 
├─pages
│      Welcome.jsp
│     
└─WEB-INF
    │  struts-config.xml
    │  validation.xml
    │  web.xml
    │ 
    ├─classes
    │  │  MessageResources.properties
    │  │ 
    │  └─hello    
    │              helloAction.class
    │              helloActionForm.class
    │             
    ├─lib
    │      antlr-2.7.2.jar
    │      commons-beanutils-1.7.0.jar
    │      commons-chain-1.1.jar
    │      commons-digester-1.8.jar
    │      commons-logging-1.0.4.jar
    │      commons-validator-1.3.1.jar
    │      oro-2.0.8.jar
    │      struts-core-1.3.8.jar
    │      struts-taglib-1.3.8.jar
    │      struts-tiles-1.3.8.jar
    │     
    └─src
        │  build.xml
        │  README.txt
        │ 
        └─java
                MessageResources.properties

然后把整个hello文件夹放到Tomcat的webapps下,访问http://localhost:8080/hello

OK!到现在为止,我们对struts的基本知识复习了一下。我们继续深入一点。换一种方式来访问类里面的属性。我们增加一个helloNameList.java

package  hello;

import  java.util.List;
import  java.util.Arrays;
import  java.lang.reflect.Array;

public   class  helloNameList {
    
public String className;
    
public String[] student;

    
public helloNameList()
        student 
= new String[]{"Tom","John","Lucy","Lily"};
        className 
= "二年三組";
    }


    
public void setClassName(String cname){
        
this.className = cname;
    }


    
public String getClassName()return className ;}

    
public String[] getStudent()return student;    }

    
public String getAstudent(int index)return student[index];}

    
public int getRank(String studentName){
        
if    (studentName.equals("Tom"))         return 1;
        
else if    (studentName.equals("John"))         return 2
        
else if    (studentName.equals("Lucy"))         return 3
        
else if    (studentName.equals("Lily"))         return 4
        
else                     return 0;
    }

}


然后扩充一下我们的index.jsp

<% @ taglib uri="http://struts.apache.org/tags-html" prefix="html"  %>
<% @ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"  %>

< BR >
  Enter Your Name:

< html:form  action  ="/sayhi"  focus ="yourName" >
    
< html:text  property ="yourName" />
    
< html:submit  property ="submit"  value ="go"   />
</ html:form >

< br >
< jsp:useBean  id ="class1"  class ="hello.helloNameList" />   <!--  先引用bean  -->
< bean:define  id  ="myClass"  name ="class1"  property ="className" /> <!--   再定义  -->
< bean:define  id  ="myName"  name  ="class1"  property ="student[1]"   />
私は
<% = myClass %> <% = myName %> です。 < br >

< bean:define  id  ="myClassmate"  name  = "class1"  property  = "astudent[2]"   /> <!--   注意不是astudent(2)  -->
< bean:define  id  ="ranking"  name  = "class1"  property  = "rank(Lucy)"   /> <!--   字符型参数的,注意与上面的区别  -->
<% = myClassmate %> is my classmate. Rank is  <% = ranking %> < br >

<!--  我们还可以定义与类无关的数据  -->
< bean:define  id ="myString"  value ="Hello"   />
< bean:define  id ="firstChar"  name  ="myString"  property  = "bytes[0]"   /> <!--  相当于调用getBytes()方法,返回byte数组的第一数据  -->
<% = firstChar %> < br >

< jsp:setProperty  name ="class1"  property ="className"  value ="Grade 2 Class 3" />  
< bean:define  id  ="myClass"  name  = "class1"  property ="className" />< br >
My Class is
<% = myClass %>

编译,配置,然后访问http://localhost:8080/hello  是不是看见乱码了。我们在index.jsp开头添加下面语句
<%@ page contentType="text/html; charset=Shift_JIS" %>
可是从bean来的数据还是乱码,提交表达后,都是乱码了。虽然通过右键➔编码➔日语(自动选择)部分正常显示,可是从bean来的数据还是乱码。那我们给result.jsp开头也添加同样的语句。这样子,提交表单后,来自bean的数据正常显示了,可是无乱怎么弄总有一方乱码的吧。为了解决这个问题,我们需要一位朋友帮忙--Filter。现在我们来介绍一下这位新朋友。

Filter
filter是struts2开始有的。当你想在收到request之前,发出response之前,想干点什么的时候,就需要它帮满了。
读书不求甚解,实践出真知。我们先用上再慢慢解说。使用filter,我们要实现javax.servlet.Filter接口,重写其中的3个方法:init,doFilter和destroy()
SetCharacterEncodingFilter.java

package  filters;

import  java.io.IOException;
import  javax.servlet.Filter;
import  javax.servlet.FilterChain;
import  javax.servlet.FilterConfig;
import  javax.servlet.ServletException;
import  javax.servlet.ServletRequest;
import  javax.servlet.ServletResponse;
import  javax.servlet.UnavailableException;

public   class  FilterEncoding  implements  Filter  {
       
private FilterConfig filterConfig=null;
       
private String encoding=null;
 
       
public void init(FilterConfig filterConfig) throws ServletException {
              
this.filterConfig=filterConfig;
              
this.encoding=filterConfig.getInitParameter("encoding");
       }

 
       
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
              String encoding
=this.encoding;
              
if(encoding!=null)
              
{
              request.setCharacterEncoding(encoding);   
              response.setCharacterEncoding(encoding);   
              }

              chain.doFilter(request, response);
       }

 
       
public void destroy() {
              
this.encoding=null;
              
this.filterConfig=null;
       }

   
}

把编译好的类文件放到WEB-INF/classes/filters下,
我们还需要配置一下web.xml,在<web-app>之间加上

< filter >
    
< filter-name > setEncoding </ filter-name >
    
< filter-class >
        filters.FilterEncoding 
    
</ filter-class >
    
< init-param >
        
< param-name > encoding </ param-name >
        
< param-value > Shift_JIS </ param-value >
    
</ init-param >
</ filter >
< filter-mapping >
    
< filter-name > setEncoding </ filter-name >
    
< url-pattern > /* </ url-pattern >
</ filter-mapping >

貌视修正了。这回bean的文字正常显示了,但是网页上写的文字,怎么也无法正常显示了。怎么回事呢。在这里又作为一道作业留给大家。

一般乱码问题可以尝试从以下方面解决。
①jsp文件本身的编码问题。
②EncodingFilter 的设定(web.xml)
③Tomcat5 以上GET出现乱码的时候,在server.xml的Connector tag里面追加
      <Connector ... useBodyEncodingForURI="true"/>                                                     
④使用Tomcat,request出现乱码的时候,在server.xml的Connector tag里面追加,编码根据encodingfilter的设定
<Connector ... URIEncoding="UTF-8"/>                                                                   

还可以参考这篇文章"struts中文问题","struts国际化问题"的终极解决方案

内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值