Struts2学习笔记1

Struts2学习笔记1

 
一.
下载
struts

2.0.1

http://struts.apache.org/downloads.html,下载struts-

2.0.1
-all.zip
,这个压缩包中包括了开发struts2所需的struts2-core.jar核心包以及其他struts2所依赖的JAR文件,另外另一些struts2的演示样例程序以及一些HTMLAPI文档。

二.试用struts

2.0.1

1. 新建一个WEBproject,将struts-

2.0.1
-all.zip
压缩包中的lib文件夹下的全部jar文件复制到WEBproject的/WEB-INF/lib文件夹下。

改动WEB-INF下的web.xml文件,添�例如以下内容:

<span>Struts2学习笔记1</span>
<
filter
>

<span>Struts2学习笔记1</span>        

<
filter-name
>
struts2
</
filter-name
>

<span>Struts2学习笔记1</span>        

<
filter-class
>

<span>Struts2学习笔记1</span>            org.apache.struts2.dispatcher.FilterDispatcher
<span>Struts2学习笔记1</span>        

</
filter-class
>

<span>Struts2学习笔记1</span>    

</
filter
>

<span>Struts2学习笔记1</span>    

<
filter-mapping
>

<span>Struts2学习笔记1</span>        

<
filter-name
>
struts2
</
filter-name
>

<span>Struts2学习笔记1</span>        

<
url-pattern
>
/*
</
url-pattern
>

<span>Struts2学习笔记1</span>    

</
filter-mapping
>

<span>Struts2学习笔记1</span>    

<!–
 这里是设置struts2标签,也能够不用设置,由于在struts-core.jar的META-INF文件夹下已经包括了
<span>Struts2学习笔记1</span>        这个tld文件,J2EE容器会自己主动地载入它 

–>

<span>Struts2学习笔记1</span>    

<
jsp-config
>

<span>Struts2学习笔记1</span>        

<
taglib
>

<span>Struts2学习笔记1</span>            

<
taglib-uri
>
/s
</
taglib-uri
>

<span>Struts2学习笔记1</span>            

<
taglib-location
>

<span>Struts2学习笔记1</span>                /WEB-INF/tlds/struts-tags.tld
<span>Struts2学习笔记1</span>            

</
taglib-location
>

<span>Struts2学习笔记1</span>        

</
taglib
>

<span>Struts2学习笔记1</span>    

</
jsp-config
>

web.xml中定义了一个struts2FilterDispathcerfilter,这个FilterDispatcher用来初始化struts2而且处理全部的WEB请求。


       2. 新建一个登录页面login.jsp

<span>Struts2学习笔记1</span>
<span>Struts2学习笔记1</span>
<%

@ page language=java contentType=text/html; charset=UTF-8
<span>Struts2学习笔记1</span>    pageEncoding
=UTF-8

%>

<span>Struts2学习笔记1</span><span>Struts2学习笔记1</span>

<%

@taglib prefix=s uri=/struts-tags
%>

<span>Struts2学习笔记1</span>

<
html
>

<span>Struts2学习笔记1</span>

<
head
>

<span>Struts2学习笔记1</span>

<
title
>
登录页面
</
title
>

<span>Struts2学习笔记1</span>

</
head
>

<span>Struts2学习笔记1</span>

<
body
>

<span>Struts2学习笔记1</span>

<
s:form 
action
=”login”
>

<span>Struts2学习笔记1</span>    

<
table 
align
=”center”
>

<span>Struts2学习笔记1</span>    

<
caption
><
h3
>
用户登录
</
h3
></
caption
>

<span>Struts2学习笔记1</span>        

<
tr
>

<span>Struts2学习笔记1</span>            

<
td
><
s:textfield 
label
=”用户名”
 name
=”username”
 
/></
td
>

<span>Struts2学习笔记1</span>        

</
tr
>

<span>Struts2学习笔记1</span>        

<
tr
>

<span>Struts2学习笔记1</span>            

<
td
><
s:password 
label
=”密  码”
 name
=”password”
 
/></
td
>

<span>Struts2学习笔记1</span>        

</
tr
>

<span>Struts2学习笔记1</span>        

<
tr 
align
=”center”
>

<span>Struts2学习笔记1</span>            

<
td
><
input 
type
=”submit”
 value
=”登录”
/></
td
><
td
><
input 
type
=”reset”
 value
=”重填”
 
/></
td
>

<span>Struts2学习笔记1</span>        

</
tr
>

<span>Struts2学习笔记1</span>    

</
table
>

<span>Struts2学习笔记1</span>

</
s:form
>

<span>Struts2学习笔记1</span>

</
body
>

<span>Struts2学习笔记1</span>

</
html
>

<span>Struts2学习笔记1</span>

3.编写Action login

<span>Struts2学习笔记1</span>
package
 org.rainlife.struts2.action;
<span>Struts2学习笔记1</span>
<span>Struts2学习笔记1</span>

import
 com.opensymphony.xwork2.ActionSupport;
<span>Struts2学习笔记1</span>
<span>Struts2学习笔记1</span><span>Struts2学习笔记1</span>

public
 
class
 LoginAction 
extends
 ActionSupport 

{
<span>Struts2学习笔记1</span>    
private String username;
<span>Struts2学习笔记1</span>
<span>Struts2学习笔记1</span>    
private String password;
<span>Struts2学习笔记1</span>
<span>Struts2学习笔记1</span>    @Override
<span>Struts2学习笔记1</span><span>Struts2学习笔记1</span>    
public String execute() throws Exception {
<span>Struts2学习笔记1</span>        
if (!(getUsername().equals(rainlife))
<span>Struts2学习笔记1</span><span>Struts2学习笔记1</span>                
&& !(getPassword().equals(rainlife))) {
<span>Struts2学习笔记1</span>            
return ERROR;
<span>Struts2学习笔记1</span><span>Struts2学习笔记1</span>        }
 else {
<span>Struts2学习笔记1</span>            
return SUCCESS;
<span>Struts2学习笔记1</span>        }

<span>Struts2学习笔记1</span>    }

<span>Struts2学习笔记1</span>
<span>Struts2学习笔记1</span><span>Struts2学习笔记1</span>    
public String getPassword() {
<span>Struts2学习笔记1</span>        
return password;
<span>Struts2学习笔记1</span>    }

<span>Struts2学习笔记1</span>
<span>Struts2学习笔记1</span><span>Struts2学习笔记1</span>    
public void setPassword(String password) {
<span>Struts2学习笔记1</span>        
this.password = password;
<span>Struts2学习笔记1</span>    }

<span>Struts2学习笔记1</span>
<span>Struts2学习笔记1</span><span>Struts2学习笔记1</span>    
public String getUsername() {
<span>Struts2学习笔记1</span>        
return username;
<span>Struts2学习笔记1</span>    }

<span>Struts2学习笔记1</span>
<span>Struts2学习笔记1</span><span>Struts2学习笔记1</span>    
public void setUsername(String username) {
<span>Struts2学习笔记1</span>        
this.username = username;
<span>Struts2学习笔记1</span>    }

<span>Struts2学习笔记1</span>
<span>Struts2学习笔记1</span>}


<span>Struts2学习笔记1</span>

在这个LoginAction类中,继承了ActionSupportActionSupport xwork2这个开源框架中的一个让action可以更加高速地工作的基类,它包括了action中很多可选服务的默认实现,可以让我们更加easy地自己定义一个action

在这里我们定义了usernamepassword两个属性并提供了对应的get/set方法。而且定义了一个execute()方法,该方法覆盖ActionSupport类中的execute()方法,能够看到,它仅仅是简单地返回一个字符串(”SUCCESS””INPUT”,而不像是在struts1中的返回一个ActionForward,这两个字符串也是在ActionSupport中定义的,在ActionSupport中定义了四个String属性,分别为SUCCESS,INPUT,ERROR,LOGIN

这样,我们的action就已经完毕了,但还存在一个问题,怎么样让struts2知道我们这个自己定义的action,而且能够在HTMLJSP页面中将动作提交给action呢?答案是须要配置struts.xml文件。

       4.配置struts.xml

<span>Struts2学习笔记1</span>
<?
xml version=”1.0″ encoding=”UTF-8″
?>

<span>Struts2学习笔记1</span>

<!
DOCTYPE struts PUBLIC
<span>Struts2学习笔记1</span>        “-//Apache Software Foundation//DTD Struts Configuration 2.0//EN”
<span>Struts2学习笔记1</span>        “http://struts.apache.org/dtds/struts-2.0.dtd”

>

<span>Struts2学习笔记1</span>

<
struts
>

<span>Struts2学习笔记1</span>    

<
package 
name
=”struts2″
 extends
=”struts-default”
>

<span>Struts2学习笔记1</span>        

<
action 
name
=”login”
 class
=”org.rainlife.struts2.action.LoginAction”
>

<span>Struts2学习笔记1</span>            

<
result 
name
=”error”
>
/error.jsp
</
result
>

<span>Struts2学习笔记1</span>            

<
result 
name
=”success”
>
/success.jsp
</
result
>
        
<span>Struts2学习笔记1</span>        

</
action
>

<span>Struts2学习笔记1</span>    

</
package
>

<span>Struts2学习笔记1</span>

</
struts
>

<span>Struts2学习笔记1</span>

在这个struts.xml配置文件里,能够发现和曾经的struts-config.xml已经全然不一样了,而在webwork的配置文件很类似。在这里,我们定义一个名name=”login”action,通过class属性指向了刚才我们创建的LoginAction类,这样,就将我们定义的action告诉给了struts2。而在HTMLJSP页面中,能够通过这个”login”这个name来将动作提交给对应的Action

假设在package中设置了namespace属性,如namespace=”/struts2”,则在JSP页面中应该将Formaction设置为”/struts2/login.action”

       5.创建error.jspsuccess.jsp

error.jsp

<span>Struts2学习笔记1</span>
<span>Struts2学习笔记1</span>
<%

@ page language=java import=java.util.* pageEncoding=UTF-8
%>

<span>Struts2学习笔记1</span>

<
html
>

<span>Struts2学习笔记1</span>    

<
head
>

<span>Struts2学习笔记1</span>        

<
title
>
错误页面
</
title
>

<span>Struts2学习笔记1</span>    

</
head
>

<span>Struts2学习笔记1</span>    

<
body
>

<span>Struts2学习笔记1</span>        您不能登录!
<span>Struts2学习笔记1</span>    

</
body
>

<span>Struts2学习笔记1</span>

</
html
>

<span>Struts2学习笔记1</span>

success.jsp

<span>Struts2学习笔记1</span>
<span>Struts2学习笔记1</span>
<%

@ page language=java import=java.util.* pageEncoding=UTF-8
%>

<span>Struts2学习笔记1</span>

<
html
>

<span>Struts2学习笔记1</span>    

<
head
>

<span>Struts2学习笔记1</span>        

<
title
>
成功页面
</
title
>

<span>Struts2学习笔记1</span>    

</
head
>

<span>Struts2学习笔记1</span>    

<
body
>

<span>Struts2学习笔记1</span>        您已经登录!
<span>Struts2学习笔记1</span>    

</
body
>

<span>Struts2学习笔记1</span>

</
html
>

<span>Struts2学习笔记1</span>

Struts2会依据在LoginAction中返回的字符串(ERRORSUCCESS)来和在struts.xml<result>中的name属性所定义的字符串匹配,并跳转到对应页面。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/119025.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号