用Eclipse 开发Dynamic Web Project应用程序(Struts练习)
软件环境:
eclipse: version=3.7.0
tomcat:apache-tomcat-6.0.37
一,创建Server
通过菜单选择File > New > Other>Server,创建Server,如下图所示。
单击“下一步”,再单击“完成”。
二,创建Dynamic Web Project项目
1,通过菜单选择File > New > Dynamic Web Project,新建项目,项目名HelloWorld,其他值默认。
- <%@ 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.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>
4,新建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;
- }
5,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="HelloWorld" extends="struts-default">
- <action name="login" class="com.test.action.LoginAction">
- <!-- result没有名字是默认的success -->
- <result name="success">/result.jsp</result>
- </action>
- </package>
- </struts>
6,新建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>
7,右击项目,Run...>Run on Server,选择刚新建的Server。