Struts2 第一篇 Hello World
来源:互联网 发布:深度卷积网络 dcnn 编辑:程序博客网 时间:2024/05/24 15:38
1、环境搭建
(1)环境配置
jdk1.6.0_45
apache-tomcat-6.0.37
struts 2.1.8
(2)所需jar包
struts2-core-2.1.8.jar
xwork-core-2.1.6.jar
ognl-2.7.3.jar
freemarker-2.3.15.jar
commons-logging-1.0.4.jar
commons-fileupload-1.2.1.jar
(3)struts.xml配置文件
(4)web.xml文件
2、Hello World走起
首先编写第一个struts2程序。
(1)web.xml
<?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"> <display-name></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>在web.xml中配置了一个struts2的过滤器,同时在url-pattern中配置了过滤模式,表示对所有的HTTP请求进行过滤。
(2)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> <package name="yy" namespace="/test" extends="struts-default"> <action name="helloWorld" class="com.struts2.study.yy.HelloWorldAction" method="execute" ><result name="success">/pages/helloWorld.jsp</result> </action> </package> </struts>此文件在src目录下。该文件是配置struts2的跳转行为的。其中package的作用类似于java中的package,方便管理action。
name属性:唯一,表示包的名称。
namespace属性:包的命名空间,作为访问路径的一部分。
extends属性:指定继承包,其中struts-default定义了struts的核心功能。
abstract属性:是否是抽象包,如果是,则不能包含action配置。
action标签各种属性说明如下:
name属性:action的名字,和package的namespace共同组成访问路径。
class属性:指定action对应的处理类。
method属性:指定action对应的处理方法。
(3)HelloWorldAction.java
package com.struts2.study.yy;/** * @author yy * @time 2013-12-23 下午3:11:32 */public class HelloWorldAction {public String info ;public void setInfo(String info) {this.info = info;}public String execute(){info = "hello, welcome to the struts2 world!";return "success";}}注意:execute方法必须返回String,对应于页面跳转的标识。
(4)helloWorld.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <body> the content is : ${info} </body></html>
注意:
此处的${info}中的info对应于setInfo方法中的Info,与定义的info属性没有关系。
(5)结果访问:http://localhost:8080/struts2study/test/helloWorld
显示内容如下:
the content is : hello, welcome to the struts2 world!
0 0
- Struts2 第一篇 Hello World
- 第一篇:Hello World!
- Hello World第一篇
- 第一篇 hello world
- 第一篇博客,Hello World
- 第一篇博客:Hello,world!
- 第一篇博客: Hello world!
- WPF自学篇--第一篇--Hello world
- 第一篇博客(Hello World)
- 第一篇笔记就像Hello world!
- SpringMVC 第一篇【万能的Hello World】
- 第一篇博客从hello world开始
- C语言基础第一篇--Hello world !
- c++入门第一篇:hello,world!
- SpringBoot入门系列:第一篇 Hello World
- SpringBoot入门系列:第一篇 Hello World
- JNI开发第一篇实现Hello World!
- SpringBoot入门系列:第一篇 Hello World
- u-boot(201009版)启动
- windows7/vs2008/32位程序/默认设置,new可分配最大空间
- 导出List列表中的数据到Excel文件,并提供下载
- Android 运行时找不到类:java.lang.NoClassDefFoundError:...
- 黑马程序员_02_模板方法模式
- Struts2 第一篇 Hello World
- android 程序初始化界面.. .
- UIPopoverController
- android.content.res.Resources$NotFoundException: String resource ID #0x0 找不到资源文件ID #0x0
- 黑马程序员_03_享元设计模式Flyweight
- Integer与int的种种比较你知道多少?
- IOS7 UITableViewCellSelectionStyleBlue设置无效的替代方案。
- word中设置页眉页脚一些方法
- ret