首先去struts的官网https://struts.apache.org/download.cgi#struts2516,下载一个2.5及以上版本的struts2,选择下载Full Distribution,里面包含很多学习文档。
建立一个java web项目,就和普通的java web项目一样的建立方法。在WEB-IINF中的lib文件夹中放入必要的jar包,都在刚下载的struts2文件中,直接将需要的jar包放入lib中,然后build path。这是项目的文件结构
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>struts2小例子</title>
</head>
<body>
<form method="post" action="login.action">
用户名:<input name="userName" type="text"/><br/>
密码:<input name="password" type="password"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>成功页面</title>
</head>
<body>
登录成功
</body>
</html>
LoginBean.java
package com.demo.bean;
public class LoginBean {
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 boolean login(String userName,String password) {
if("qq".equals(userName)&&"123".equals(password)) {
return true;
}
return false;
}
}
LoginAction.java
package com.demo.action;
import com.demo.bean.LoginBean;
public class LoginAction {
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{
LoginBean l = new LoginBean();
if(l.login(userName, password)) {
return "success";
}
return "error";
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 这个约束很重要,一定要加上,不然会报错 -->
<!-- 不同的struts版本对应的配置文件中的DTD信息会有一定的差异。一定要使用与版本相对应的DTD信息 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 所有的Action配置都应放在package下,name定义包名,extends定义继承包空间 -->
<package name="xx" extends="struts-default">
<!-- Action配置可以有多对;name是对业务控制器命名
在表单中指定的action的名字需要与该名字一致;class指定Action类的位置 -->
<action name="login" class="com.demo.action.LoginAction">
<!-- 定义两个逻辑视图与物理资源之间的映射
name值是Action中execute()方法返回的结果,即逻辑视图 -->
<result name="success">/page/success.jsp</result>
<result name="error">/page/login.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>demo</display-name>
<!-- 设置欢迎界面,登录网站时首先展示的页面 -->
<welcome-file-list>
<welcome-file>/page/login.jsp</welcome-file>
</welcome-file-list>
<filter>
<!-- 配置struts2核心控制器的名称 -->
<filter-name>struts</filter-name>
<filter-class>
<!-- 这是struts-2.5及以上版本的控制器 -->
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<!-- struts2控制器的名称 -->
<filter-name>struts</filter-name>
<!-- 拦截所有的以action结尾的请求,让静态资源可以正常加载 -->
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
运行结果