就像初学java一样,依旧写一个hello world,不过这个是通过struts2来控制它如何显示在页面上。
软件环境:jdk 6.0 , tomcat 6.0, struts 2.3.12。
1. 从strut2s官网http://struts.apache.org/development/2.x/index.html 来下载 struts 2.3.12的 相关文件,包括完整的压缩包、应用示例、文档、依赖库、源代码等。如下图所示:
2. 新建一个web工程,打开struts-2.3.12-apps.zip将其中的struts2-blank.war下的lib中的jar包放到工程lib中,或者将其引入工程。
3. 现在我们捋顺我们的思路:
1) 创建一个存储welcome信息的类;
2) 创建展现该message的页面HelloWorld.jsp;
3) 创建用于一个用于控制用户、model层、view层三者交互的action类;
4) 创建配置文件struts.xml来映射view层与action类使之关联起来;
4. 代码实现:
1)model类:
package com.hsy.struts2; public class MessageStore { private String message; public MessageStore() { super(); this.setMessage("[struts2 test]: hello world!"); } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
2) HelloWorld.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Hello World!</title> </head> <body> <h2><s:property value="messageStore.message" /></h2> </body> </html>
3)Action类
package com.hsy.struts2; import com.opensymphony.xwork2.ActionSupport; public class HelloWorldAction extends ActionSupport { private static final long serialVersionUID = 1L; private MessageStore messageStore; public MessageStore getMessageStore() { return messageStore; } public void setMessageStore(MessageStore messageStore) { this.messageStore = messageStore; } public String execute() throws Exception { messageStore = new MessageStore(); return SUCCESS; } }
4)struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="basicstruts2" extends="struts-default"> <action name="index"> <result>/index.jsp</result> </action> <action name="hello" class="com.hsy.struts2.HelloWorldAction" method="execute"> <result name="success">/HelloWorld.jsp</result> </action> </package> </struts>
5) 在index.jsp中创建URL Action
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Basic Struts 2 Application - Welcome</title> </head> <body> <h1>Welcome To Struts 2!</h1> <p><a href="<s:url action='hello'/>">Hello World</a></p> </body> </html>
6) web.xml中配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Struts Blank</display-name> <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> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> </web-app>