创建一个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, ...
随机推荐
- Java中的简单工厂模式
举两个例子以快速明白Java中的简单 工厂模式: 女娲抟土造人话说:“天地开辟,未有人民,女娲抟土为人.”女娲需要用土造出一个个的人,但在女娲造出人之前,人的概念只存在于女娲的思想里面.女娲造人,这就 ...
- [firefox+plug-n-hack]轻松地配置burpsuite代理https流量
http://zone.wooyun.org/content/25982 需要用到firefox的插件plug-n-hack下载https://raw.githubusercontent.com/mo ...
- python_Day1_基础知识开篇
一.python安装 1)windows上同时安装python2.0和python3.0配置 (1)在python官网下载windows版本python2.0和python3.0安装包 官网地址:ht ...
- JSP 相关试题(四)
简答 1.有人说:servlet和 JavaBean都是java类,可以互换使用,请您回答,在MVC中,控制器部分是否可以使用JavaBean完成?为什么? 不能.因为javabean是普通的java ...
- linux文件锁
http://blog.chinaunix.net/uid-25324849-id-3077304.html 在SHELL中实现文件锁,有两种简单的方式.(1)一是利用普通文件,在脚本启动时检查特定文 ...
- unity,下面两个协程不等价
//代码1 IEnumerator A(){ Debug.Log(“hi1”); { yield return new WaitForSeconds(1f); Debug.Log(“hi2”); } ...
- Python文件处理之文件写入方式与写缓存(三)
Python的open的写入方式有: write(str):将str写入文件 writelines(sequence of strings):写多行到文件,参数为可迭代对象 首先来看下writelin ...
- HDP2.4安装(三):MySql安装
在安装Ambari时,Ambari默认的数据库是ProstgreSQL,对ProstgreSQL不太熟悉,选择使用MySql. 但Centos 7 默认支持的是MariaDB数据库. MariaDB是 ...
- Hibernate入门学习(一)
一.Hibernate是什么 Hibernate主要用来实现Java对象和数据表之间的映射,除此之外还提供数据查询和获取数据的方法,可以大幅度减少开发时人工使用SQL和JDBC处理数据的时间.Hibe ...
- 【转】c#实现文件下载
需求:点击SAVE,去ajax去后台下载文件,完成后出现保存对话框,然后直接下载. 解决方案:下载完成后用JS转到向到另一个页面,在这页面Page_Load里用C#Response对象直接下载. 下载 ...