strucst 第一程序

下面我们一起来搭建第一个struts程序
第一步:配置structs.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<!--下面是配置的内容-->
   <package name="itcast" namespace="/test" extends="struts-default">
        <action name="helloworld"
<!-- method是当这个Action被调用时候执行的方法-->
class="com.liyong.action.HelloWorldAction" method="execute" >
<result name="success">/WEB-INF/page/hello.jsp</result>
<!-- 访问路径 http://localhost:8080/Structs2/test/helloworld
        </action>
    </package>
</struts>
第二步:编写一个Action
注意:类命名、方法和class="com.liyong.action.HelloWorldAction" method="execute"相同
public class HelloWorldAction {

private String msg;

public String getMessage() {
return msg;
}
        //这个方法来自于struts.xml文件中的method="execute"
public String execute()
{
msg="hello world!";
//这里必须返回一个String 这个结果表示返回的.jsp页面
return "success";
}
}
第三步:编写hello.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'hello.jsp' starting page</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">   
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>
 
  <body>
<!--${}这个表达式意思通过反射机制调用getMessage()方法这个方法在我们的Action类中-->
    ${message}
  </body>
</html>
第四步:不是我们的程序
注意:
<1、在部署的时候出现版本不对的情况,你的修改jdk的版本
<2、访问我们的Action路径
<!-- 访问路径 http://localhost:8080/Structs2/test/helloworld -->
Structs2是我们的项目名
test是上面structs.xml中的命名空间
helloworld是structs.xml的Action名称

你可能感兴趣的:(程序)