Struts入门配置

Struts入门配置可以简单6步完成:

step1:从官网下载jar包

step2:新建dynamic web project项目,导入必要的jar包

step3:编写action类

step4:添加配置到struts.xml文件

step5:添加配置到web.xml文件

step6:定义jsp页面


下面详细每个步骤:

step1:从官网下载jar包

强调一点,如果需要更好的学习,官网、官方文档、API等是首选项,包括下载jar包也是,下面给出官网下载地址:

https://mirrors.tuna.tsinghua.edu.cn/apache/struts/2.5.13/struts-2.5.13-all.zip

下载后解压,可以找到lib目录中有很多jar包,对于入门学习并用不了这么多,在step2中会选几个必要的添加

step2:新建dynamic web project项目,导入必要的jar包

在eclipse中新建dynamic web project,项目名称为learnStruts2。导入Struts2必要的jar包,截图如下:



step3:编写action类

package control;

import com.opensymphony.xwork2.ActionSupport;

public class FirstStrutsAction extends ActionSupport{
	private String msg; 

	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String execute(){
		msg = "Hello world!!!";
		
		System.out.println("Hello world!!!");
		return SUCCESS;
	}
	
	public String myFun(){
		msg = "my fun!!!";
		
		System.out.println("my fun!!!");
		return SUCCESS;
	}
}

step4:添加配置到struts.xml文件


   
   


   
   
	
    
    
	
    
    
	
    
    
	
    
    
	
    
    
	
    
    
	
    
    
	
    
    

	
    
    
	    
     
     
		
     
     
			
      
      
       
       /helloWorld.jsp
      
      
		
     
     
		
     
     
		
     
     
			
      
      
       
       /helloWorld.jsp
      
      
		
     
     
	
	
    
    

   
   

step5:添加配置到web.xml文件

    
    

    
    
  
     
     
      
      learnStruts2
     
     
  
     
     
    
      
      
       
       index.html
      
      
    
      
      
       
       index.htm
      
      
    
      
      
       
       index.jsp
      
      
    
      
      
       
       default.html
      
      
    
      
      
       
       default.htm
      
      
    
      
      
       
       default.jsp
      
      
  
     
     
  
  
     
     
    
      
      
       
       struts2
      
      
    
      
      
       
       org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
      
      
  
     
     
  
     
     
    
      
      
       
       struts2
      
      
    
      
      
       
       *.action
      
      
  
     
     

    
    

step6:定义jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




    
    
Insert title here



${msg}



至此框架已经配置好了,然后发布至tomcat或jboss,启动服务。在浏览器中访问,输出不同信息,地址如下:
http://localhost:8080/learnStruts2/helloWorld.action
http://localhost:8080/learnStruts2/myFun.action
最后项目中的文件目录截图:


参考文献:
struts官网API

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值