1. Eclipse工程图
2. 下载Struts2
地址:http://labs.xiaonei.com/apache-mirror/struts/binaries/struts-2.1.6-all.zip
解压后将struts-2.1.6\apps\struts2-blank-2.1.6\WEB-INF\lib目录下所有的jar加进工程的classpath
其实,根据官方文档,依赖的jar应该是
commons-fileupload, commons-io, commons-loggin, freemarker, ongl, struts2-core, xwork
3. Source
cn.edu.dlut.ssdut.pojo.User.java
package cn.edu.dlut.ssdut.pojo; public class User { private String id; private String password; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
cn.edu.dlut.ssdut.action.LoginAction.java
package cn.edu.dlut.ssdut.action; import com.opensymphony.xwork2.ActionSupport; import cn.edu.dlut.ssdut.pojo.User; public class LoginAction extends ActionSupport { private User user; public void setUser(User user) { this.user = user; } public User getUser() { return user; } @Override public String execute() throws Exception { if (!isValid(user.getId()) || !isValid(user.getPassword())) { return INPUT; } return SUCCESS; } private boolean isValid(String str) { if (str != null && !"".equals(str.trim())) { return true; } return false; } }
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <title> Login Page </title> <head> <title>Login Page</title> </head> <body> <s:form action="hello/login" method="post"> <s:textfield name="user.id" label="UserId"/> <s:password name="user.password" label="Password"/> <s:submit value="Login"/> </s:form> </body> </html>
home.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <head> <title>Login Page</title> </head> <body> Hello, <s:property value="user.id" /> </body> </html>
以下是配置文件web.xml和struts.xml
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"> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
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" namespace="/hello"> <action name="login" class="cn.edu.dlut.ssdut.action.LoginAction"> <result name="success">/home.jsp</result> <result name="input">/index.jsp</result> </action> <!-- Add your actions here --> </package> </struts>
4. 编译及运行