struts2注解配置注意事项

本文详细介绍了 Struts2 的注解配置方法,并针对常见问题提供了解决方案。主要内容包括:如何正确配置 action 类,理解 Struts2 默认查找 action 的规则,以及在不同场景下配置 action 的两种方式。通过实例演示,帮助开发者快速掌握 Struts2 的使用技巧。

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

今天在写struts2的注解时遇到了低级错误下面给个分享:

     总结action配置:


package com.test.web.actons;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionSupport;

@Namespace(value="/test")
public class TestAction extends ActionSupport{
	private static final long serialVersionUID = 2118537853660540192L;
	
	@Action(value="login",results={@Result(location="bookList.jsp")})
	public String login() throws Exception{
		System.out.println("hello struts2!!");
		return SUCCESS;
	}
}


这段配置相信大家都很熟悉,但是重点来了:

     struts2默认会去找*.actions或*.action包下的action,这点很关键。

所以我总结了配置注解有两种方式:

     A.将你的action文件放在*.actions或*.action包下就可以了,注解生效(注意一定是在*.actions或*.action包下哦)

     B.你的action文件可以随便放,那么就需要在web.xml中配置参数:

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 	<!-- 添加struts2、sitemesh支持 -->
    <filter>
        <filter-name>struts</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
		<init-param> 
			<param-name>actionPackages</param-name> 
			<param-value>com.test.action</param-value> 
		</init-param> 
    </filter>
    <filter-mapping>
        <filter-name>struts</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> 	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


 

 

 

 

 

 

 

 

 

以上是这次错误的总结,希望能给各位朋友带来帮助!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值