使用struts2 建立Helloworld

1环境搭配:

tomcat , eclipse jee;

tomcat 解压到C盘; 配置好环境变量CATALINA_BASE:tomcat目录,CATALINA_HOME:tomcat目录;path;tomcat目录\bin;

eclipse 中配置tomcat:server中加入tomcat的目录;

2,helloworld:

新建dynamic web project;在WEB-INF里的lib文件夹中导入struts2的jar包:commons-fileupload-1.3.1.jar,commons-io-2.2.jar,commons-lang3-3.1.jar,commons-logging-1.1.3.jar,core-0.6.2.jar,freemarker-2.3.19.jar,javassist-3.11.0.GA.jar,ognl-3.0.6.jar,servlet-api.jar(此包在tomcat里),struts2-core-2.3.16.3.jar,xwork-core-2.3.16.3.jar

一、src目录里新建struts.xml:内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "">
<struts>
	<!-- 配置包,名称为bookcode -->
	<package name="<span style="font-family: Arial, Helvetica, sans-serif;">bookcode</span><span style="font-family: Arial, Helvetica, sans-serif;">" extends='struts-default' namespace="/"></span>
		<!-- 配置Action -->
		<action name="HelloWorld" class="cn.com.test.HelloWorld" method = "execute">
			<!-- 配置返回结果 -->
			<result name="success">/success.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
	</package>
</struts>
二、Webcontent中WEB-INF中建立web.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">
	<display-name>TestHelloWorld</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.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

三、在src中建立包cn.com.test,在包里建立HelloWorld.java文件,内容如下:


package cn.com.test;


import com.opensymphony.xwork2.Action;



public class HelloWorld implements Action {
<span style="white-space:pre">	</span>// 定义msg属性
<span style="white-space:pre">	</span>private String msg;


<span style="white-space:pre">	</span>// msg的get方法
<span style="white-space:pre">	</span>public String getMessage() {
<span style="white-space:pre">		</span>return msg;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>// msg的set方法
<span style="white-space:pre">	</span>public void setMsg(String msg) {
<span style="white-space:pre">		</span>this.msg = msg;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>// Action的execute()处理方法
<span style="white-space:pre">	</span>public String execute() {
<span style="white-space:pre">		</span>// 判断条件
<span style="white-space:pre">		</span>if (getMessage().equals("")) {
<span style="white-space:pre">			</span>// 显示错误信息
<span style="white-space:pre">			</span>System.out.println("no String input!");
<span style="white-space:pre">			</span>// 返回错误结果
<span style="white-space:pre">			</span>return "error";
<span style="white-space:pre">		</span>} else {
<span style="white-space:pre">			</span>// 显示用户输入的信息
<span style="white-space:pre">			</span>System.out.println(getMessage());
<span style="white-space:pre">			</span>// 返回一个处理成功结果
<span style="white-space:pre">			</span>return "success";
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
}



四、在WEB-INF中建立HelloWorld.jsp

<%@ page language="java" contentType="text/html; charset=utf8"
	pageEncoding="utf8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<!-- form的action指向定义的action名称 -->
	<form id="form1" name="form1" method="post" action="HelloWorld">
		<p>
			输入信息: <label> <input name="msg" type="text" id="username" />
			</label>
		</p>
		<p>
			<label> <input type="submit" name="Submit" value="提交" />
			</label>
		</p>
	</form>
</body>
</html>
五、在WEB-INF中建立 success.jsp 和 error.jsp

success.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
		${Message} 恭喜你,struts显示成功
</body>
</html>

error.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Sorry, u need to try more
</body>
</html>

六、run on server

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值