1,我需要建立一个工程,名称为“StrutsTutorial”
2,引入lib。在webroot/WEB-INF/lib文件夹下放入六个jar文件:commons-fileupload-1.2.1.jar;commons-io-1.3.2.jar;freemarker-2.3.15.jar;ognl-2.7.3.jar;struts-core-2.1.8.1.jar;xwork-core-2.1.6.jar
3,配置web.xml文件。在WEB-INF文件夹下配置web.xml文件。
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>StrutsTutorial</display-name>
<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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
4,修改jsp文件。
首先,修改该index.jsp文件
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
在webroot文件夹下新建一个文件夹pages
HelloWorld.jsp
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts 2 Hello World Application!</title>
</head>
<body>
<h2><s:property value="message" /></h2>
<p>Current date and time is: <b><s:property value="currentTime" /></b>
</body>
</html>
login.jsp
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts 2 Login Application!</title>
</head>
<body>
<s:form action="doLogin" method="POST">
<tr>
<td colspan="2">Login</td>
</tr>
<tr>
<td colspan="2"><s:actionerror /></td>
</tr>
<s:textfield name="user.username" label="Login name" />
<s:password name="user.password" label="Password" />
<s:submit value="Login" align="center" />
</s:form>
</body>
</html>
loginsuccess.jsp
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Login Success</title>
</head>
<body>
<p align="center"><font color="#000080" size="5">Login Successful
</font></p>
Hello <s:property value="user.username"/>!<s:actionmessage />
</body>
</html>
5 在src文件夹下面新建一个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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<package name="helloworld" namespace="/helloworld"
extends="struts-default">
<action name="HelloWorld"
class="cn.hxex.struts.helloworld.action.HelloWorldAction">
<result>/pages/HelloWorld.jsp</result>
</action>
<!-- Add actions here -->
</package>
<package name="login" namespace="/login" extends="struts-default">
<action name="toLogin">
<result>/pages/login.jsp</result>
</action>
<action name="doLogin" class="cn.hxex.struts.login.action.LoginAction">
<result name="input">/pages/login.jsp</result>
<result name="error">/pages/login.jsp</result>
<result name="success">/pages/loginsuccess.jsp</result>
</action>
</package>
</struts>
6 在src文件夹下新建三个package
分别包裹三个java文件
HelloWorldAction.java文件
package cn.hxex.struts.helloworld.action;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class HelloWorldAction extends ActionSupport {
public static final String MESSAGE = "Struts 2 Hello World Tutorial!";
public String execute() throws Exception {
// 设置显示到页面的信息
setMessage(MESSAGE);
return SUCCESS;
}
private String message;
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public String getCurrentTime() {
return new Date().toString();
}
}
LoginAction.java
package cn.hxex.struts.login.action;
import cn.hxex.struts.login.bean.User;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class LoginAction extends ActionSupport {
private User user;
public String execute() throws Exception {
boolean checkResult = "Admin".equals( user.getUsername() )
&& "Admin".equals( user.getPassword() );
if( checkResult ) {
addActionMessage("Welcome you to here!");
return SUCCESS;
} else {
addActionError("Invalid user name or password! Please try again!");
return ERROR;
}
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
User.java
package cn.hxex.struts.login.bean;
public class User {
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;
}
}
package cn.hxex.struts.login.bean;
public class User {
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;
}
}
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts 2 Login Application!</title>
</head>
<body>
<s:form action="doLogin" method="POST">
<tr>
<td colspan="2">Login</td>
</tr>
<tr>
<td colspan="2"><s:actionerror /></td>
</tr>
<s:textfield name="user.username" label="Login name" />
<s:password name="user.password" label="Password" />
<s:submit value="Login" align="center" />
</s:form>
</body>
</html>