一两年没弄 JAVA 了 , 今天开始重新捡起来 . 这两天准备看一下 struts2
一个简单的 struts2 入门程序
1. 从 www.apache.org 下载 struts 包
2. 新建项目 test
从下载的 struts 包中拷贝如下文件加到 test 工程的 lib 中 :
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.0.4.jar
freemarker-2.3.15.jar
ognl-2.7.3.jar
struts2-core-2.1.8.1.jar
xwork-core-2.1.6.jar
3. 新建一个类 com.test.Login.java
package com.test;
public class Login {
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(){
if ( userName .equals( "aa" ) && password .equals( "bb" )){
return "sucess" ;
} else {
return "fail" ;
}
}
}
注意 : userName 和 password 要跟登陆页上的定义一致
4. 新建三个 jsp 文件
(1) 登陆页面 index.jsp
<%@ page language = "java" import = "java.util.*" pageEncoding = "utf-8" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< title > index </ title >
</ head >
< body >
< form action = "login.action" >
用户名 : < input type = "text" name = "userName" />< br />
密码 : < input type = "password" name = "password" />< br />
< input type = "submit" name = "submit" />
</ form >
</ body >
</ html >
(2) 登陆成功 sucess.jsp
<%@ page language = "java" import = "java.util.*" pageEncoding = "utf-8" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< title > sucess </ title >
</ head >
< body >
登陆成功 !
</ body >
</ html >
(3) 登陆失败 fail.jsp
<%@ page language = "java" import = "java.util.*" pageEncoding = "utf-8" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< title > fail </ title >
</ head >
< body >
登陆失败 < br />
< a href = "javascript:history.go(-1)" > 重新登陆 </ a >
</ body >
</ html >
5. 在 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>
<constant name = "struts.enable.DynamicMethodInvocation" value = "false"/>
<constant name = "struts.devMode" value = "false"/>
<package name = "example" extends = "struts-default">
<action name = "login" class = "com.test.Login">
<result name = "sucess"> /sucess .jsp </result>
<result name = "fail">/fail.jsp</result>
</action>
</package>
</struts>
6. 修改web.xml, 增加一个过滤器( 在struts1 中是新加一个servlet)
<? 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 >
<welcome-file-list>
<welcome-file> index.jsp </welcome-file>
</welcome-file-list>
</web-app>