struts建立工程helloworld

Java web环境:Tomcat + Jdk +eclipse java EE
创建一个能运行的java web工程,记得勾选上web.xml
下载struts库,目前最新2.5-2.16 all.zip 将lib下的jar包
http://mirrors.tuna.tsinghua.edu.cn/apache/struts/2.5.16/struts-2.5.16-all.zip
放到当前的工程lib文件夹
下,右键build path,add to path
 
工程项目如下:
 

hello包下的helloworld.java:
package hello;
public class helloworld {
   private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String execute() {
if(getMessage().isEmpty()) {
return "error";
}else {
    return "success";
}
}  
}

src文件夹下struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    <struts>
        <package name="helloworld" namespace="/" extends="struts-default">
        <action  name="helloworld" class="hello.helloworld">
    <result  name="success">/showMessage.jsp</result>
    <result  name="error">/nomessage.jsp</result>
        </action>
        </package>
    </struts>

WEB-INF:文件夹下web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

  <display-name>helloworld</display-name>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

 

<!-- 配置核心拦截器-->

<filter>

    <!-- Filter的名字-->

    <filter-name>struts2</filter-name>

    <!-- Filter的实现类struts2.5以前可能有所不同-->

    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>

<filter-mapping>

    <filter-name>struts2</filter-name>

    <!--拦截所有的url -->

    <url-pattern>/*</url-pattern>

</filter-mapping> 

</web-app>

 

 

 

WebContent文件夹下

inputMessage.jsp:

<%@ 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>input message page</title>

</head>

<body>

<form action="helloworld.action" method="post">

Message:<input name="message" type="text">

<input type="submit"value="提交">

</form>

</body>

</html>

 

nomessage.jsp:

<%@ 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>noMessage</title>

</head>

<body>

  no message was input!

</body>

</html>

showmessage.jsp
 

<%@ 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">

<%@ taglib prefix="s" uri="/struts-tags"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>show message page</title>

</head>

<body>

    the input message is: <s:property value="message" />

</body>

</html>

 

运行结果

 

转载于:https://www.cnblogs.com/zhaocundang/p/9029556.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值