创建一个struts2的HelloWorld
1、下载struts2的jar包
http://struts.apache.org/download.cgi#struts255
下载一个稳定版本Struts 2.3.31
里面提供了maven jar
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.31</version>
</dependency>
2、创建一个动态web工程导入这些jar包
修改web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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 Demo</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>
可以参照struts2-blank中的配置
3、在src目录下创建struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" /> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <global-results>
<result name="error">/WEB-INF/jsp/error.jsp</result>
</global-results> <global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="error" />
</global-exception-mappings> <action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action> <action name="products-input">
<result>/WEB-INF/pages/products.jsp</result>
</action> <action name="products-detail" class="com.hy.Products">
<result name="success">/WEB-INF/pages/detail.jsp</result>
</action> </package> <!-- Add packages here --> </struts>
参照truts2-blank中的配置
struts.enable.DynamicMethodInvocation指的是是否支持模糊匹配方法名
这里面有一个pojo
package com.hy;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class Products{
private String productID;
private String productName;
private String productDesc;
public String getProductID() {
return productID;
}
public void setProductID(String productID) {
this.productID = productID;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
public String detail() {
return SUCCESS;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
两个跳转的页面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>商品录入</title>
</head>
<body>
<form action="products-detail" method="post">
productID:<input type="text" name="productID" /><br>
productName:<input type="text" name="productName" /><br>
productDesc:<input type="text" name="productDesc" /><br>
<input type="submit" value="submit"><br>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>商品详细</title>
</head>
<body>
productID:<input type="text" name="productID" value="${productID}"/><br>
productName:<input type="text" name="productName" value="${productName}"/><br>
productDesc:<input type="text" name="productDesc" value="${productName}"/><br>
</body>
</html>
大概就这样
注意:
导入struts2-blank到eclipse时报错,这是一个maven工程
首先要安装maven插件(高版本eclipse自带)
http://maven.apache.org/download.cgi
选择一个较新的版本下载
下载完成后解压到本地,配置m2_home
D:\Program Files\apache-maven-3.3.9
再配置bin目录到path变量中
测试 cmd mvn -version
现在import exist maven projects 导入后项目编译报错

选择直接忽略。具体原因未知
这时候maven update project 把改下的jar包下载到maven jar包仓库中
启动tomcat会发现struts2核心包classnotfound
需要把maven dependence加入到部署环境中
运行之后可能会出现jsp报错,这是因为maven中包含了较低版本的tomcat jar删除依赖后启动成功
代码链接:http://pan.baidu.com/s/1qXDXli0
创建一个struts2的HelloWorld的更多相关文章
- struts2官方 中文教程 系列一:创建一个struts2 web Application
先贴了本帖地址,以免被爬 http://www.cnblogs.com/linghaoxinpian/p/6898779.html 本教程将会通过安装struts2框架来创建一个简单的应用程序.虽然 ...
- 创建一个Spring的HelloWorld程序
Spring IOC IOC指的是控制反转,把对象的创建.初始化.销毁等工作都交给Spring容器.由spring容器来控制对象的生命周期.下图能够说明我们传统创建类的方式和使用Spring之后的差别 ...
- 2.第一个Struts2程序-HelloWorld程序
1.新建Web Project项目:Study_Struts2 2.新建HelloWordAction.java类 3.复制struts.xml文件到src目录下,配置struts.xml文件内容如下 ...
- Node.js快速创建一个Express应用的几个步骤
Node.js 的 Express 框架学习 转载和参考地址: https://developer.mozilla.org/zh-CN/docs/Learn/Server-side/Express_N ...
- SpringMVC基础入门,创建一个HelloWorld程序
ref:http://www.admin10000.com/document/6436.html 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要 ...
- 简单创建一个完整的struts2框架小程序
要完成一个struts2框架的搭建, 1.首先应该从官网上下载最新的jar包,网络连接:http://struts.apache.org/download.cgi#struts2514.1,选择下载F ...
- 创建一个spring helloworld
1.下载所需要的jar包 http://projects.spring.io/spring-framework/ 这里使用了maven方式给出jar <dependencies> < ...
- 如何创建一个简单的struts2程序
如何创建一个简单的Struts2程序 “计应134(实验班) 凌豪” 1.创建一个新的Web项目test(File->new->Web Project) 2.Struts2框架的核心配置文 ...
- 图解使用IDEA创建第一个Java程序HelloWorld
前几次给大家分享了怎么在自己的电脑上配置 java 环境,准备工作做好了,我们就要开始我们真正的编码学习了.下面介绍使用 IDEA 创建我们的第一个 HelloWorld 程序. 1.打开 IDEA, ...
随机推荐
- vb6中webbrowser控件树转换备忘
Dim doc As HTMLDocument Set doc = WebBrowser1.Document Dim inputs As IHTMLElementCollection Set inpu ...
- 发现PDF Transformer+转换的图像字体小了如何处理
ABBYY PDF Transformer+转换的原始图像字体太小怎么办?为了获得最佳文本识别效果,请用较高的分辨率扫描用极小字体打印的文档,否则很容易在转换识别时出错.下面小编就给大家讲讲该怎么解决 ...
- Oboe 提升web 用户体验以及性能
Oboe 地址:http://oboejs.com/ 1.安装 bower bower install oboe 2.使用,ajax 模式 oboe('/myapp/things.json') ...
- jQuery的图像裁剪插件Jcrop
1.最基本使用方法 html代码部分: <img src="demo_files/flowers.gif" id="demoImage"/> ...
- 关于getClass().getClassLoader()
关于getClass().getClassLoader() InputStream is = getClass().getClassLoader().getResourceAsStre ...
- mysql 1449 : The user specified as a definer ('root'@'%') does not exist 解决方法
权限问题,授权 给 root 所有sql 权限 mysql> grant all privileges on *.* to root@"%" identified by & ...
- sqlplus将查询结果重定向到文件,不输出到屏幕
--每行的字符数目 --该参数设置每页输出的行数.n=0表示不产生新页 --显示和拷贝long类型值的最大宽度的设置,最大值2G --sqlplus检索long类型值的增量大小.由于内存的限制 可按增 ...
- openstack(liberty): devstack中的iniset/iniget函数分析
这个ini开头的函数在devstack的启动配置中用的非常多,他主要负责.ini文件的配置,这个过程包括对相关ini文件的添加,注释,删除,获取信息,多行信息获取等. 这里主要说的iniset和ini ...
- 对自己的文件使用keystore签名
keytool 对jar包签名步骤: 1.将程序打成jar包. 2.生成keystore数字证书keytool -genkey -keystore xxx.keystore -alias xxx -v ...
- 剑指offer系列33-----把二叉树打印成多行
[题目]从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 方法一:直接打印 package com.exe7.offer; import java.util.LinkedList; i ...