我认为学习一门新技术最快的方法就是,用这门技术做一个简单的完整的实例。下面是我学习Struts2开始时写的一个登陆程序,供学习struts2的新手看到一下。
下面的截图是该应用用到的jar包:
[img]http://dl.iteye.com/upload/attachment/176560/3861abe2-3a8e-3b6f-a0c1-d15d28d4c7f4.jpg[/img]
web.xml的配置:
struts.xml的配置:
LoginAction.java代码如下:
下面是jsp页面:
index.jsp页面内容:
success.jsp页面的内容:
下面的截图是该应用用到的jar包:
[img]http://dl.iteye.com/upload/attachment/176560/3861abe2-3a8e-3b6f-a0c1-d15d28d4c7f4.jpg[/img]
web.xml的配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="2.5">
<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.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="strutsTest" extends="struts-default" >
<action name="login" class="cn.edu.hist.action.LoginAction">
<result name="success" >/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
LoginAction.java代码如下:
package cn.edu.hist.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Action;
/**
* Created by IntelliJ IDEA.
* User: jiaxy
* Date: 2009-11-27
* Time: 11:38:55
* DESC:
*/
public class LoginAction implements Action {
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 {
if(getUsername().equals("guojia")&& getPassword().equals("123")){
ActionContext.getContext().getSession().put("username",getUsername());
return "success";
}else{
return "error";
}
}
}
下面是jsp页面:
index.jsp页面内容:
<%--
Created by IntelliJ IDEA.
User: jiaxy
Date: 2009-11-27
Time: 11:32:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>测试</title></head>
<body>
<form action="login" method="post">
用户名:<input type="text" size="12" name="username"/><br>
密 码:<input type="password" size="12" name="password"/><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
success.jsp页面的内容:
<%--
Created by IntelliJ IDEA.
User: jiaxy
Date: 2009-11-27
Time: 14:10:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>登陆页面</title></head>
<body>
欢迎,${sessionScope.username},你已成功登陆!
</body>
</html>