基于Struts2开发Java Web项目主要包括以下步骤:
- 在web.xml中配置核心控制器StrutsPrepareAndExecuteFilter。
- 设计和编写视图组件(JSP页面)。
- 编写用来实现视图组件的业务逻辑组件JavaBean。
- 编写视图组件对应的业务控制器组件Action,在该Action中调用业务逻辑组件JavaBean,进行业务逻辑处理。
- 配置业务控制器Action,即修改struts.xml配置文件。在struts.xml配置文件中配置逻辑视图与物理视图之间的跳转关系。
基于Struts2的登录系统
项目的文件结构:
LoginAction.java:
package loginAction;
import loginBean.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 lb=new LoginBean();
if(lb.login(userName, passWord)) {
return "success";
}
else {
return "error";
}
}
}
LoginBean.java:
package loginBean;
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) {
boolean b=false;
if(userName.equals("hjw")&&passWord.equals("123")) {
b=true;
}
else {
b=false;
}
return b;
}
}
struts.xml:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<include file="example.xml"/>
<package name="zzf" extends="struts-default">
<action name="login" class="loginAction.LoginAction">
<result name="error">/login/login.jsp</result>
<result name="success">/login/success.jsp</result>
</action>
</package>
</struts>
login.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>基于Struts2的登录系统应用</title>
</head>
<body>
<form method="post" action="login">
用户名:<input name="userName" type="text" size="26">
<br>
密码:<input name="passWord" type="password" size="26">
<br>
<input type="submit" value="登录">
</form>
<hr>
</body>
</html>
success.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录成功页面</title>
</head>
<body>
<h1>你登录成功,欢迎你!!!</h1>
</body>
</html>
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/javaeee/web-app_3_1.xsd"
id="WebApp_ID"
version="3.1" >
<filter>
<!-- 配置Struts2核心控制器的名字 -->
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<!-- Struts2控制器的名字 -->
<filter-name>struts2</filter-name>
<!-- 拦截所有的URL请求-->
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
登录界面:
登录成功界面: