package com.wuyudong.helloworld.model; public class MessageStore { private String message; public MessageStore(String msg){ this.setMessage(msg); } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
创建Action类HelloWorldAction,充当Controller
package com.wuyudong.helloworld.action; import com.opensymphony.xwork2.ActionSupport; import com.wuyudong.helloworld.model.MessageStore; public class HelloWorldAction extends ActionSupport { private static final long serialVersionUID = -4958566543551999157L; private MessageStore msgStore; @Override public String execute() throws Exception { msgStore = new MessageStore("HelloWorld!"); return SUCCESS; } public MessageStore getMsgStore() { return msgStore; } public void setMsgStore(MessageStore msgStore) { this.msgStore = msgStore; } }
配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee " id="WebApp_ID" version="3.0"> <display-name>12.01</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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> </web-app>
运行后如图所示
推荐阅读: