1.建立一个webproject –>struts2
2.导入相应的jar包(可以参考blank项目)
3.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4.创建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>
<package name="default" extends="struts-default">
<action name="login" class="org.action.LoginAction">
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
5.编写loginaction.java
package org.action;
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{
return "success";
}
}
然后与请求映射关联起来
stuts.xml
<action name="login" class="org.action.LoginAction">
<result name="success">/welcome.jsp</result>
</action>
6.创建视图login.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录页面</title>
</head>
<body>
<form action="login.action" method="post">
<table align="center" border=1 width="300" bgcolor=#F5FFE1>
<tr><td colspan="2" align="center" width="250" bgcolor="#CCCCFF">用户登录界面</td></tr>
<tr><td width="50">姓名:</td><td><input type="text" name="username"/></td></tr>
<tr><td width="50">密码:</td><td><input type="password" name="password" size=22/></td></tr>
<tr><td align="center" colspan=2><input type="submit" value="登录"/><input type="reset" value="重置" /></td></tr>
</table>
</form>
</body>
</html>
7.创建成功视图
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>欢迎界面</title>
</head>
<body bgcolor=#F5FFE1>
欢迎您!${username}
</body>
</html>