以第一篇的HelloWorld來修改
※XML設定
pom.xml
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.24</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.3.24.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.13.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.13.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.2.13.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>3.2.13.RELEASE</version> </dependency>
※主要是struts2-spring-plugin-2.x.x.x.jar,但我其他的spring-x都是3.0.5,應該也是可以執行,但我的spring是下載3.2.13的,乾脆全部統一比較不會有問題
web.xml
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext*.xml,classpath*:applicationContext*.xml</param-value> </context-param> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
※注意放的位置有順序,錯誤訊息會提示
※context-param是在設定有多個applicationContext開頭,.xml結尾的,一起抓進來,可以只設定一個,多個用「,」隔開
struts.xml
<constant name="struts.objectFactory" value="spring" /> <constant name="struts.devMode" value="false" /> <!-- <constant name="struts.objectFactory.spring.autoWire" value="type" /> --> <package extends="struts-default" name="basicstruts2"> <action name="index"> <result>/index.jsp</result> </action> <action class="xxx" method="execute" name="hello"> <result name="success">/HelloWorld.jsp</result> </action> </package>
※原本的action class是要打package.class,現在可以隨便打,只要對應到applicationContext.xml的id或name就可以了,struts.devMode也可以不用
applicationContext.xml
<bean id="xxx" class="action.HelloWorldAction" /> <bean id="msg" class="model.MessageStore" />
※struts.xml有一行註解了,依參考文件拖到最下面有一張表,還有七個可以做設定,我目前這樣就可以run成功了
※Annotation設定
pom.xml增加
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>2.3.24</version> </dependency>
※版本和struts2-core一樣,不然有可能啟動時出現「java.lang.NoClassDefFoundError: org/apache/commons/lang/xwork/StringUtils」,找不到類別的錯誤
HelloWorldAction.java
@Namespace(value = "/") @Controller public class HelloWorldAction extends ActionSupport { private MessageStore messageStore; // setter/getter... @Action(value = "hello", results = { @Result(name = "success", location = "/HelloWorld.jsp") }) @Override public String execute() throws Exception { messageStore = new MessageStore(); return SUCCESS; } }
※注意使用annotation時,action要放在包名叫struts、struts2、action、actions四者之一,否者就會出「There is no Action mapped for namespace [/] xxx」的錯,這四個名字可以有子包,只要和applicationContext.xml的class對應好就行
※struts.xml留第一行就好,applicationContext.xml空的即可,MessageStore上面增加@Service就OK了
沒有留言:
張貼留言