今天在写struts2的注解时遇到了低级错误下面给个分享: java
总结action配置: web
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; } }
这段配置相信你们都很熟悉,可是重点来了: apache
struts2默认会去找*.actions或*.action包下的action,这点很关键。 app
因此我总结了配置注解有两种方式: jsp
A.将你的action文件放在*.actions或*.action包下就能够了,注解生效(注意必定是在*.actions或*.action包下哦) url
B.你的action文件能够随便放,那么就须要在web.xml中配置参数: spa
<?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"> <!-- 添加struts二、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.servlet</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>
以上是此次低级错误的总结,但愿给朋友带来帮助! code