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>

 

运行结果

 

posted @ 2018-05-12 19:03  zhaocundang  阅读(197)  评论(0编辑  收藏  举报