【案例】、请用struts2开源框架实现如下功能:
注解:下文中所提及的源目录文件,为struts源代码下载文件(本文用的代码为:struts-2.3.15.1)后,\struts-2.3.15.1\apps\struts2-blank.war项目解压的文件
步骤一:创建一个Web工程(struts2--2)
步骤二:将原生态的struts中的jar包(\struts2-blank\WEB-INF\lib),拷贝到struts2—2项目中WebRoot/WEB-INF/lib下,将原生态的struts中的struts.xml配置文件(\struts2-blank\WEB-INF\src\java\struts.xml)拷贝到src源目录下。
步骤三:通过将原生态的struts中的web.xml文件(\struts2-blank\WEB-INF)拷贝到项目中WebRoot/WEB-INF路径下。
步骤四:创建login.jsp页面,创建路径为:WebRoot下
<%@ page language="java"contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPEhtml PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>Login Page</h4>
<formaction="user_login.action"method="post">
UserName:<inputtype="text"name="username"/>
<br/><br/>
PassWord:<inputtype="password"name="password">
<br/><br/>
<inputtype="submit"value="login"/>
</form>
count On Line No. : ${applicationScope.count}
<br/><br/>
count On Line No.2 : ${sessionScope.count }
<br/><br/>
</body>
</html>
步骤五:创建login_detail.jsp页面,创建路径为:WebRoot下
<%@ page language="java"contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPEhtml PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Welcome:${sessionScope.username }
<br/><br/>
<br/><br/>
Welcome:${sessionScope.password }
<br/><br/>
count On Line No.1 :${applicationScope.count }
<br/><br/>
count On Line No.2 : ${sessionScope.count }
<br/><br/>
<ahref="logout.do">logout</a>
</body>
</html>
步骤六:配置struts.xml文件
A、 配置 Struts 可以受理的请求的扩展名
<constant name="struts.action.extension"value="action,do,"></constant>
B、 配置Login_Ui的action的struts.xml配置信息
C、 配置user_login的action的struts.xml配置信息
D、 配置logout的action的struts.xml配置信息
配置好所有的参数后struts.xml代码为:
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstruts PUBLIC
"-//Apache Software Foundation//DTDStruts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 配置 Struts可以受理的请求的扩展名 -->
<constantname="struts.action.extension"value="action,do,"></constant>
<packagename="loginBeginning"extends="struts-default"namespace="/">
<actionname="Login_Ui"class="com.guan.qing.LoginAction">
<resultname="success">/login.jsp</result>
</action>
<actionname="user_login"class="com.guan.qing.LoginDetailAction">
<resultname="success">/login_detail.jsp</result>
</action>
<actionname="logout"class="com.guan.qing.LoginOutAction">
<resultname="success">/login.jsp</result>
</action>
</package>
</struts>
步骤七:配置struts.xml配置文件中action对应的后台Action类,路径为:(struts2—2/com/guan/qing)目录下:
A、 怎样获得从JSP页面转发过来的参数信息
B、 LoginAction.java
C、 LoginDetailAction.java
D、 LoginOutAction.java
类LoginAction.java文件:
packagecom.guan.qing;
public class LoginAction {
public String execute(){
return"success";
}
}
类LoginDetailAction.java文件:
packagecom.guan.qing;
importjava.util.Map;
importcom.opensymphony.xwork2.ActionContext;
public class LoginDetailAction {
private Stringusername;
private Stringpassword;
private Integercount;
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setCount(Integer count) {
this.count = count;
}
public String execute(){
//1、获得ActionContext对象
ActionContext actionContext =ActionContext.getContext();
//2、获得session对象
Map<String,Object> session =actionContext.getSession();
//3、通过session获取,从JSP页面转发过来的参数信息(获取并绑定)
session.put("username",username);
session.put("password",password);
System.out.println("JSP页面的用户名(username):" + username);
System.out.println("JSP页面的密 码(password):" +password);
//用户数目加1
count = (Integer)session.get("count");
if(count==null){
count = 0;
}
count++;
session.put("count", count);
return"success";
}
}
类LoginOutAction.java文件:
packagecom.guan.qing;
importjava.util.Map;
importorg.apache.struts2.dispatcher.SessionMap;
import com.opensymphony.xwork2.ActionContext;
public class LoginOutAction {
public String execute(){
//1、获得ActionContext对象
ActionContext actionContext =ActionContext.getContext();
//2、获得session对象
Map<String, Object> session =actionContext.getSession();
Integer count = (Integer)session.get("count");
if(count!=null && count>0){
count--;
session.put("count", count);
}
//2、session失效,强转为SessionMap,调用invalidate方法
((SessionMap<String,Object>)session).invalidate();
return"success";
}
}
测试效果如下: