第一步,导入工程所需jar包
我自己用的是struts-2.5.16-min-lib.zip,解压导入jar。
下载链接:https://pan.baidu.com/s/1f5NGEZfheJu1GvhVC3GYuQ 密码:o9v8
第二步,实现登录功能
package com.bingcao.test;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String username;
private String password;
public String login() throws Exception {
if (isInvalid(getUsername())) return INPUT;
if (isInvalid(getPassword())) return INPUT;
System.out.println("登录成功");
return SUCCESS;
}
private boolean isInvalid(String value) {
return (value == null || value.length() == 0);
}
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;
}
}
第三步,配置文件
Struts2配置
这里我用了引用文件的方式,所有有两个xml文件,struts.xml和example.xml。
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<global-allowed-methods>regex:.*</global-allowed-methods>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="error" />
</global-exception-mappings>
</package>
<include file="example.xml" />
<!-- Add packages here -->
</struts>
example.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!-- 限制上传文件的大小,第一种方式:通过修改Struts2的常量。 在default.properties中配的Struts.multipart.maxSize=2097152 -->
<constant name="struts.multipart.maxSize" value="5242880"></constant>
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
<package name="topic" extends="default">
<action name="Topic_*" class="com.bingcao.test.TopicAction" method="{1}">
<result name="list">/index.jsp</result>
</action>
</package>
<package name="user" extends="default">
<action name="Login_*" method="{1}" class="com.bingcao.test.LoginAction">
<result>/test/Welcome.jsp</result>
<result name="input">/test/Login.jsp</result>
</action>
</package>
</struts>
web.xml配置
从官方demo中看到,struts2.5有两个监听器,struts-prepare和struts-execute,目前自己分不清,都用上了。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>sh_tieba</display-name>
<filter>
<filter-name>struts-prepare</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class>
</filter>
<filter>
<filter-name>struts-execute</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-prepare</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts-execute</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
第四步,jsp的登录页面
Login.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Sign On</title>
</head>
<body>
<s:form action="Login_login">
<s:textfield key="username"/>
<s:password key="password" />
<s:submit/>
</s:form>
</body>
</html>
Welcome.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h3>登录成功</h3>
</body>
</html>
我的文件部署:
其中遇到的问题
原来是因为struts2.5为了提升安全性,添加了一个allowed-methods
属性,此属性指定可被调用的方法。
allowed-methods属性还可以使用正则表达式配置:
<global-allowed-methods>regex:.*</global-allowed-methods>