Struts2学习笔记(1)----用户登录的实现

今天开始学习Struts2,用的教材是李刚的《Struts2.1权威指南》,刚学习,就碰到了不少麻烦,特在此记录下来,以备后用。

(1)首先,去http://struts.apache.org 下载struts2的完整版,解压后有很多jar包

(2)在MyEclipse中新建一个WebProject,特别要注意,在创建新Project时,会要求你选择J2EE Specification Level,默认的是J2EE1.4,要选择Jave EE 5.0才可以

(3)导入jar包。在工程名上点右键,选择Build Path,Configure Build Path,Libraries,Add External Jars,导入必须的类库

开始时,我按照书上所述,只导入了struts2-core-2.2.1.jar,xwork-core-2.2.1.jar,ognl-3.0.jar这三个包,但部署在Tomcat上之后,网站却启动不了,Running显示false,点击Start后,提示“FAIL - Application at context path /TestStruts could not be started ”,在网上搜了下,说可能是缺少必要的类库,于是,我仿照另一个project,导入了其他几个包,重新部署后,网站便正常启动了。

Referenced Libraries里是我导入的jar包,整个工程的文档结构如下图:

Struts2学习笔记(1)----用户登录的实现

(4)配置文件:struts.xml

View Code
   
     
<? 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.2.dtd"
>

< struts >
< package name ="strutsqs" extends ="struts-default" >
< action name ="login" class ="com.jp.action.LoginAction" >
< result name ="error" > /error.jsp </ result >
< result name ="success" > /welcome.jsp </ result >
</ action >
</ package >
</ struts >

web.xml

View Code
   
     
<? xml version="1.0" encoding="UTF-8" ?>
< web-app version ="2.5"
xmlns
="http://java.sun.com/xml/ns/javaee"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>

< welcome-file-list >
< welcome-file > login.jsp </ welcome-file >
</ welcome-file-list >

< 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 >

login.jsp

View Code
   
     
<% @ page language = " java " import = " java.util.* " pageEncoding = " UTF-8 " %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + " :// "
+ request.getServerName() + " : " + request.getServerPort()
+ path + " / " ;
%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< base href ="<%=basePath%>" >

< title > My JSP 'index.jsp' starting page </ title >
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</ head >

< body >
< form action ="login.action" method ="post" >
< table align ="center" >
< caption >
< h3 >
用户登录
</ h3 >
</ caption >
< tr >
< td >
用户名:
< input type ="text" name ="username" />
</ td >
</ tr >
< tr >
< td >
&nbsp;&nbsp; 码:
< input type ="password" name ="password" />
</ td >
</ tr >
< tr align ="center" >
< td colspan ="2" >
< input type ="submit" value ="登录" />
< input type ="reset" value ="重置" />
</ td >
</ tr >
</ table >
</ form >
</ body >
</ html >

welcome.jsp

View Code
   
     
<% @ page language = " java " import = " java.util.* " pageEncoding = " UTF-8 " %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + " :// " + request.getServerName() + " : " + request.getServerPort() + path + " / " ;
%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< base href ="<%=basePath%>" >

< title > My JSP 'welcome.jsp' starting page </ title >

< meta http-equiv ="pragma" content ="no-cache" >
< meta http-equiv ="cache-control" content ="no-cache" >
< meta http-equiv ="expires" content ="0" >
< meta http-equiv ="keywords" content ="keyword1,keyword2,keyword3" >
< meta http-equiv ="description" content ="This is my page" >
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</ head >

< body >
欢迎,${sessionScope.user},你已经成功登录
< br >
</ body >
</ html >

LoginAction.java

View Code
   
     
package com.jp.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

// 实现了Action接口
public class LoginAction implements Action{
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( " jp " ) && getPassword().equals( " jp " )) {
// 通过ActionContext对象访问Web应用的HTTP Session
ActionContext.getContext().getSession().put( " user " , username);

// 使用了Action接口中定义的SUCCESS、ERROR常量
return SUCCESS;
}
else
return ERROR;
}
}

你可能感兴趣的:(struts2)