http://javaweb.group.iteye.com/group/wiki/1505-struts2-under-helloworld---how-to-make-the-first-of-the-running-struts2

在刚刚下载的struts2-1-6目录下的lib中复制出如下六个文件
  commons-logging-1.0.4.jar

  freemarker-2.3.8.jar 

  ognl-2.6.11.jar 

  struts2-core-2.0.6.jar

  xwork-2.0.1.jar

以及(因为是struts2-1-6版本的。所以一下这个文件也必不可少)

commons-fileupload-1.2.1

然后粘贴到WebRoot/WEB-INF/lib即可;

第四步:

WebRoot目录下新建一个login.jsp

代码如下

login.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<form action="login.action" method="post">
username: <input name="username" type="text"><br>
password: <input name="password" type="password"><br> <input type="submit" value="submit">
</form>
</body>
</html>

第五步:
修改WEB-INF下的web.xml文件

代码如下

web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.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>
</web-app>

第六步:
新建action

在src目录下新建包com.test.action

在包中新建一个action代码如下

LoginAction.java

    package com.test.action;  

    public class LoginAction
{ //getter和setter方法 就是根据这里的方法名来匹配客户端的信息
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";
} //对应表单上的
private String username;
private String password;
}

第七步:
struts配置文件

在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> <package name="struts2" extends="struts-default">
<action name="login" class="com.test.action.LoginAction">
<!-- result没有名字是默认的success -->
<result name="success">/result.jsp</result>
</action>
</package>
</struts>

注意,struts.xml中有句话是
!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

红色这句可能会报错,解决的方法是 将“http://”字样去掉 其他我不知道还有什么方法,有高手知道请指点一二;

第八步;

新建result文件

在WebRoot目录下新建一个result.jsp文件 代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'result.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
helloworld
username: ${requestScope.username }<br>
password: ${requestScope.password }
</body>
</html>

最新文章

  1. Oracle用法集锦
  2. 洛谷P1901 发射站
  3. Android无法收到指定广播的问题总结
  4. nyist0j 35 表达式求值
  5. mysql5.7 设置远程访问
  6. java之集合Collection 详解之4
  7. 理解 Memory barrier(内存屏障)无锁环形队列
  8. 【Linux】gdb调试
  9. 第十五周翻译-《Pro SQL Server Internals, 2nd edition》
  10. linux 中 修改权限的命令 chmod
  11. LeetCode算法题-String Compression(Java实现)
  12. 2 Servlet 细节
  13. 【计算机网络】OSI七层模型图解
  14. Python开发 基礎知識 2.變量 ( *arg, **kwargs )
  15. eclipse中配置server
  16. hive的jdbc使用
  17. [SDOI2011]消耗战(虚树+树形动规)
  18. allegro 基本步骤
  19. 【Beta阶段】第三次Scrum Meeting!
  20. bzoj1061 NOI2018 志愿者招募——solution

热门文章

  1. [MODx] 8. Snippet get data, chunk display
  2. Mysql shell 控制台---mysqlsh
  3. HttpClient 4.3.* 上传带中文文件名文件文件名乱码问题的解决
  4. 实例源码--Android日历实例源码
  5. Xcode常用快捷键总结
  6. 计算openlayers两点之间的距离
  7. 关于Git中分支merge和rebase的适用场景及区别
  8. eval()函数用法详解
  9. 【linux错误解决】MySQL远程连接ERROR 2003 (HY000):Can&#39;t connect to MySQL server on&#39;XXXXX&#39;的问题
  10. Spring之IOC容器初始化过程