资源包下载:http://download.csdn.net/source/830515
配置过程如下:
1。将commons-logging-1.1.jar freemarker-2.3.8.jar ognl-2.6.11.jar struts2-core-2.0.6.jar xwork-2.0.1.jar 复制到lib下
2。修改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的FilterDispathcer的Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="jCuckoo" extends="struts-default">
<action name="Login" class="jCuckoo.LoginAction">
<result name="input">/login.jsp</result>
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
LoginAction
public class LoginAction extends ActionSupport {
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() throws Exception {
if (getUsername().equals("admin") && getPassword().equals("admin")) {
ActionContext.getContext().getSession().put("user", getUsername());
return SUCCESS;
} else {
return ERROR;
}
}
@Override
public void validate() {
if (getUsername() == null || getUsername().trim().equals("")) {
addFieldError("username", getText("user.required"));
}
if (getPassword() == null || getPassword().trim().equals("")) {
addFieldError("password", getText("pass.required"));
}
}
}
messageResource.properties
l oginPage=登陆页面
errorPage=错误页面
succPage=成功页面
failTip=对不起,您不能登录!
succTip=欢迎,{0},您已经登录!
viewLink=查看作者李刚已出版的图书
bookPageTitle=作者李刚已出版的图书
bookName=书名:
user=用户名
pass=密 码
login=登陆
user.required=用户名必须输入!
pass.required=密码必须输入!
login.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title><s:text name="loginPage" />
</title>
</head>
<body>
<s:form action="Login">
<s:textfield name="username" key="user" />
<s:textfield name="password" key="pass" />
<s:submit key="login" />
</s:form>
</body>
</html>
error.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title><s:text name="errorPage"/></title>
</head>
<body>
<s:text name="failTip"/>
</body>
</html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title><s:text name="succPage"/></title>
</head>
<body>
<s:text name="succTip">
<s:param>${sessionScope.user}</s:param>
</s:text><br>
<a href="GetBooks.action"><s:text name="viewLink"/></a>
</body>
</html>