用struts2實現簡單的登入
阿新 • • 發佈:2019-02-08
首先先寫一個簡單的的登入介面,程式碼如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>登入</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 >
<s:form action="login">
<s:textfield name="username" key="使用者名稱"/>
<s:textfield name="password" key="密碼"/>
<s:submit key="登入dd"/>
</s:form>
</body>
</html>
然後,寫一個Action,命名為LoginAction,程式碼如下
package com.dhy.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
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;
}
@Override
public String execute() throws Exception {
System.out.println(getUsername()+getPassword());
// TODO Auto-generated method stub
ActionContext ctx=ActionContext.getContext();
//通過ActionContext訪問application範圍的屬性值
Integer counter=(Integer)ctx.getApplication().get("counter");
if(counter==null){
counter=1;
}else{
counter=counter+1;
}
ctx.getApplication().put("counter", counter);
//通過ActionContext設定session範圍的屬性
ctx.getSession().put("user", username);
if(getUsername().equals("jocean")&&getPassword().equals("jocean")){
//通過ActionContext設定request範圍的屬性
System.out.println("in");
ctx.put("tip","伺服器提示:您已經登入成功");
return SUCCESS;
}
//通過ActionContext設定request範圍的屬性
ctx.put("tip", "伺服器提示:登入失敗!");
return ERROR;
}
}
配置struts2.xml,其配置如下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 指定全域性國際化資原始檔 -->
<!-- add -->
<!-- <constant name="struts.custom.i18n.resources" value="mess"/> -->
<!-- End -->
<!-- <constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" /> -->
<package name="crazyit" namespace="/" extends="struts-default">
<action name="login" class="com.dhy.action.LoginAction">
<result name="error">/WEB-INF/content/error.jsp</result>
<result name="success">/WEB-INF/content/success.jsp</result>
</action>
</package>
<!-- <include file="example.xml"/> -->
<!-- Add packages here -->
</struts>
然後,寫success.jsp和error.jsp,其中success.jsp的程式碼如下:
<%@ 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>成功頁面</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>
訪問次數:${applicationScope.counter}<br/>
${sessionScope.user},您已經登入!<br/>
${requestScope.tip}
</body>
</html>
error.jsp的程式碼如下:
<%@ 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 'error.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>
${requestScope.tip}
</body>
</html>
web.xml的配置如下:
<?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">
<!-- struts2配置 ,新增過濾器,如果是通過工具新增,會自動生成-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- End -->
</web-app>
目錄結構如下圖所示:
執行結果如下: