struts 2学习进阶 初识struts2(一)

来源:互联网 发布:幼圆字体for mac下载 编辑:程序博客网 时间:2024/05/21 08:03

 好久没有写代码了,最近准备学习一下struts2,我准备把我的学习路程写下来。呵呵

Struts2与Struts1有很大的不同,Struts2是与webwork共同开发的,在Struts2中有很多xwork的jar包。看资料上说Struts2的核心是拦截器。

本人菜鸟一个,还指望着敲代码混饭吃呢。废话不多,那就开始吧。

在apache的网站上下载struts2的包,其中apps文件夹中包含了几个struts2的例子程序,docs是文档,lib是包含的jar文件,如果你的jdk是1.4需要在项目中加入j4文件夹的jar包,src是struts2的源文件

在eclipse中新建web工程,将struts2的lib中的struts2-core.jar,xwork.jar,fremarker.jar,ongl.jar引入工程中,

配置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"
>
    
    
<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>
</web-app>

 

 新建两个jsp页面,login.jsp

 

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding
="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
    
<form action="login.action" method="post">
        username
:<input type="text" name="username" />
        password
:<input type="password" name="password"/>
        
<input type="submit"/>
    
</form>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding
="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
    username:$
{requestScope.username }
    password:$
{requestScope.password }
</body>
</html>

在src目录下建立LoginAction.java

package com.xie.struts2;

public class LoginAction {
    
    
private String username;
    
    
private String password;

    
public String getUsername() {
        
return username;
    }


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


    
public String getPassword() {
        
return password;
    }


    
public void setPassword(String password) {
        
this.password = password;
    }

    
    
public String execute() {
        
return "success";
    }

}

在src下建立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>
    
<package name="struts2" extends="struts-default">
        
<action name="login" class="com.xie.struts2.LoginAction">
            
<result name="success">/success.jsp</result>
            
<result name="failer">/login.jsp</result>
        
</action>
    
</package>
</struts>

将工程部署到服务器,启动服务器。

上述只是个最简单的一个struts程序

原创粉丝点击