Struts 2.x基本环境搭建
2013-04-03 11:43
295 查看
就像初学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类:
2) HelloWorld.jsp
3)Action类
4)struts.xml
5) 在index.jsp中创建URL Action
6) web.xml中配置
软件环境: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>
相关文章推荐
- 最新版使用SSH框架(Struts2.x + Spring4.x + Hibernate5.x)搭建JavaWeb开发环境实例
- Struts2.x+Spring3.x+Hibernate4.x 整合(一)Struts环境搭建
- (转)Struts2+JFreeChart 环境搭建个基本用法!
- JAVA从零单排1-----搭建开发环境、基本语法
- spring+struts+hibernate集成环境(基于maven项目搭建)
- 快速搭建Struts1.x开发环境详细步骤与讲解
- Java当中的基本概念和环境搭建
- Hibernate基本开发环境搭建
- 对struts2的基本知识和环境的搭建(配图解)
- 使用Maven搭建Struts2+Spring3+Hibernate4的整合开发环境
- Eclipse下搭建struts、spring、hibernate开发环境
- 基于struts环境下的jquery easyui环境搭建
- HLS 基本知识及环境搭建
- spark2.x 独立集群环境搭建 | 适用于spark集群环境搭建
- linux系统最基本开发环境的搭建
- Hibernate基本环境搭建以及增删改和加载
- struts1.3环境搭建示例
- 使用Maven搭建Struts2+Spring3+Hibernate4的整合开发环境
- Spring3-hibernate3.3-struts2.1.8----环境搭建(2)
- .Net Core(一)环境搭建与基本使用