1. 新建web工程:web_ssm
2. 导入jar包:
3. web.xml
<!-- 配置struts2 -->
<filter>
<filter-name>struts</filter-name>
<filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
4.java(仅仅给出action)
package com.login.action;
import java.util.Map;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
/**
* 登陆鉴权Action类
*
* @author fanjm
* @version 1.0
* @since 2017年02月02日
*/
public class LoginAction extends ActionSupport implements RequestAware,SessionAware, ApplicationAware{
private static final long serialVersionUID = 2408145375650667872L;
/** log */
/*private static final Logger logger = LoggerFactory
.getLogger(LoginAction.class);*/
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
private String username;
private String pwd;
public String login(){
String rtn = ERROR;
if("admin".equals(username)&&"admin".equals(pwd)){
rtn = SUCCESS;
}
return rtn;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Map<String, Object> getRequest() {
return request;
}
public void setRequest(Map<String, Object> request) {
this.request = request;
}
public Map<String, Object> getSession() {
return session;
}
public void setSession(Map<String, Object> session) {
this.session = session;
}
public Map<String, Object> getApplication() {
return application;
}
public void setApplication(Map<String, Object> application) {
this.application = application;
}
}
5.1 struts文件目录结构
5.2 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.i18n.encoding" value="GBK"></constant>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<include file="struts-config/struts_login.xml" />
<!-- Add packages here -->
</struts>
5.3 struts_login.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>
<package name="default" namespace="/as" extends="struts-default">
<!--
自定义拦截器栈
<interceptors>
<interceptor-stack name="timertokenstack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="timer"/>
<interceptor-ref name="auth"/>
</interceptor-stack>
<interceptor name="auth" class="com.interceptor.AuthInterceptor"></interceptor>
</interceptors>
-->
<global-results>
<result name="error">/WEB-INF/jsp/fail.jsp</result>
</global-results>
<action name="login" class="com.login.action.LoginAction">
<result name="success">/WEB-INF/jsp/success.jsp</result>
</action>
</package>
</struts>
6. index.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 'index.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">
-->
<script src="${pageContext.request.contextPath}/js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(function(){
$("#submit").click(function(){
$("#form1").attr("action","<%=basePath%>as/login!login.action");
$("#form1").submit();
});
});
</script>
</head>
<body>
This is my JSP page. <br>
<div id='max'>
<form id="form1" name="form1">
<input type="text" name="username" />
<input type="password" name="pwd" />
<button id="submit">submit</button>
</form>
</div>
</body>
</html>
注:<base href="<%=basePath%>">
对$("#form1").attr("action","<%=basePath%>as/login!login.action");
不起作用。
7.整体目录结构