小毅也玩struts2之--->HelloWorld程序

Posted on 2008-10-09 04:20 H2O 阅读(267) 评论(0)  编辑  收藏 所属分类: struts HelloWorld程序" trackback:ping="http://www.blogjava.net/xiaoyi/services/trackbacks/233262.aspx" /> -->
项目结构
五个struts2必备jar包,可以到http://www.struts.apache.org下载最新的struts2,struts2采用过滤器org.apache.struts2.dispatcher.FilterDispatcher来过滤客户端发送给服务器的所有请求。struts2.0是strtus1.*与webwork的结合。struts2.0会自动到工程下classes下寻找struts.xml(struts2.0的配置文件)来解析配置的action等东东。废话少说,代码如下:
开发步骤:
1、 下载struts2.0并添加核心jar包
2、写页面
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
  
<head>
    
<title>struts的Hello World程序</title>
  
</head>
  
<body>
       
<form name='testform' method='post' action="login.action">
       用户名:
<input name='username'><br>
        
密码:&nbsp;&nbsp;&nbsp;&nbsp;<input name='pwd' type
='password'><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        
<input type='submit' value=' 提  交 '>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

        
&nbsp;<input type='reset' value=' 重  置 '>
       
</form>
  
</body>
</html>

show.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
    
<title>显示结果</title>
</head>
<body>
    
<h3>用户名--->${requestScope.username}<br>
        密码--->${requestScope.pwd}
<br>
</body>
</html>

3、写action
package com.yz.struts2.actions;

public class loginAction 
{
    
    
private
 String username;
    
private
 String pwd;
    
public String getUsername() 
{
        
return
 username;
    }

    
public void setUsername(String username) {
        
this.username =
 username;
    }

    
public String getPwd() {
        
return
 pwd;
    }

    
public void setPwd(String pwd) {
        
this.pwd =
 pwd;
    }

    
    
public String helloWorld(){
        System.out.println(
"用户名--->"+
username);
        System.out.println(
"密码--->"+
pwd);
        
return "ok"//查找名字为ok的result,相当于struts1.*的foward名

    }

}

4、配置struts2.0开发环境---struts.xml和web.xml
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
    
<!-- struts2采用过滤器过滤客户端发送给服务器的所有请求 -->
    
<filter>
        
<filter-name>struts2</filter-name>
        
<filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        
</filter-class>
    
</filter>
    
<filter-mapping>
        
<filter-name>struts2</filter-name>
        
<url-pattern>/*</url-pattern>
    
</filter-mapping>
    
<welcome-file-list>
        
<welcome-file>index.jsp</welcome-file>
    
</welcome-file-list>
</web-app>
struts.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>
<!-- struts2会自动到classes下找struts.xml,
直接放在src下工具会自动把src下的文件编译到classes下 
dtd头信息表示 sturs为根标签
-->
    
<!-- struts2这个包继承sturs2内置的包struts-default -->
    
<package name="struts2" extends="struts-default">
        
<!-- 配置action
            name: 对应index.jsp页面中form的action= login.action 的login
            struts默认所有以点action结尾的请求交给struts处理,因为继承自webwork的特性,习惯于这样。。
            class: action对应的类,包名点类名全路径(com.yz.struts2.loginAction)
            method:请求该action时自动执行的方法,如果没有配置默认执行execute方法
         
-->
        
<action 
            
name="login" 
            class
="com.yz.struts2.actions.loginAction"
            method
="helloWorld" 
            converter
=""
        
>
        
<!-- result默认的name为success -->
        
<result name="ok">/show.jsp</result>
        
</action>
        
    
</package>
</struts>

只有注册用户登录后才能发表评论。


网站导航:
 

posts - 0, comments - 21, trackbacks - 0, articles - 101

Copyright © H2O