`

struts入门

阅读更多

struts开发环境搭建
   1、找到开发struts需要的jar包,(6个必不可少的)
   2、新建并且配置struts。xml(放置在src目录下)
   3.配置web.xml(struts2的框架是通过Filter启动的,和struts1采用的Servlet不同)
  
  struts2的配置
  <?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" class="cn.itcast.action.HelloWorldAction" method="execute" >
   <result name="success">/WEB-INF/page/hello.jsp</result>
        </action>
    </package>
 <!--添加更多的包-->
  </struts>
  分析1:使用包来管理Action,Action就是Form表单的action:使用包来管理Action,Action就是Form表单的action,把数据提交给谁。
  1、配置包时要指明name属性,保证name属性的唯一。
  2、包的namespace属性作为访问该包下的
  Action路径的一部分。上面的例子中,要在工程名后面加上/test/helloworld。
  3、extends,包要继承struts-default,它保证了struts2的核心功能。
  4、一个包被定义为抽象,那么这个包下面不能有Action节点,只能被继承。
  5、Action的name作为访问路径的一部分。
  6、class属性就是Action的类,通过后面的method属性访问。
  7、result的name为后面为显示界面的路径的映射。
 
  package cn.itcast.action;

public class HelloWorldAction {
private String msg;
 
 public String getMessage() {
  return msg;
 }

 public String execute(){
  msg = "我的第一个struts2应用";
  return "success";
 }
}

/folder/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>
    ${message }
  </body>
</html>


  
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics