所用工具:Myeclipse2014,Tomcat8.520,Struts2.3.16.1。(网上都有)。
1.打开Meclipse2014
2,打开Tomcat服务器。F:\Struts\apache-tomcat-8.5.20-windows-x64\apache-tomcat-8.5.20\bin在bin目录下面有一个startup.bat.(如果打开只是一闪就自动退出的,百度,这里不多讲解。)
3.创建新项目,Web project.
1.在src右键创一个struts.xml文件。在WebRoot->WEB-INF右键创一个web.xml文件。2.在WEB-INF->lib放入tomcat.jar包,包的位置在F:\Struts\apache-tomcat-8.5.20-windows-x64\apache-tomcat-8.5.20\lib。(你下载Tomcat的lib目录下)。全部复制过去就好!3.
在WEB-INF右键创一个登录界面,一个登录成功界面Hello.jsp和lzt.jsp 4 .在src右键新建包,写上包的名字,例如。com.struts.lzt在创建新类-Test1.。
如上代码!!
struts.xml代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default11" namespace="/" extends="struts-default">
<global-results>
<result name="success">
/lzt.jsp
</result>
<result name="login">
/Hello.jsp
</result>
</global-results>
<action name="hello" class = "com.struts.lzt.Test1">
</action>
</package>
</struts>
(result的name决定程序跳转到哪个jsp界面,action-name:是调用action时输入路径的组成部分,输入网址的时候输入它,会自动读取此时result name ,然后跳转到相应的界面。 action–class:action所在的路径(包名+类名);)
Test1.java代码:
package com.struts.lzt;
import com.opensymphony.xwork2.ActionSupport;
public class Test1 extends ActionSupport{
public String password;
public String username;
public void getUsername(String username){
this.username=username;
}
public String setUsername(){
return username;
}
public void getPassword(String password){
this.password=password;
}
public String setPassword(){
return password;
}
@Override
public String execute(){
if("11".equals(username) && "11".equals(password))
return "success";
else
return "login";
}
}
(给出相应输入账号密码的 result-name)
web.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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">
<display-name>Struts Blank</display-name>
<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>
Hello.jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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 'index.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">
<!--
-->
</head>
<body>
<s:form action="/hello" method="post">
<s:label value="系统登陆"></s:label>
<s:textfield name="username" label="账号" />
<s:password name="password" label="密码" />
<s:submit value="登录" />
</s:form>
</body>
</html>
(s:form action=”/hello” method=”post”这句话就是每次登录后会自动运行http://localhost:8080/项目名字/hello)
lzt.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 'lzt.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>
success. <br>
</body>
</html>
1.运行前,配置tomcat服务器。在Myeclipse里面配置.
点击Wimdow->Proferneces->Myeclipse->Serves->Tomcat 然后选择版本,推荐最新版本 ,像我的话是Tomcat8.0,。Tomcat home directory 按Browse 选择你安装的Tomcar路径。再选择左上角的Enable ,按下apply。配置好了。
2.项目创建好后, 按下图上所示图标!
、
选择自己刚创建的项目,,点击Add,会出现下面的图片,在Server上选择自己的Tomcat版本。确定好,再按下右边的图片(配置图标的右边!),启动Tomcat,成功的标志是如下图片。
(有点累,我有一篇文章就是写这个的,复制累死我了)!
!!!
开始测试,运行项目,先试一下http://localhost:8080/,正常启动是如下画面
再http://localhost:8080/项目名字/Hello.jsp
出现登录界面
输入的账号密码是在Test.java类里面。
输入正确的账号密码会跳转到lzt.jsp界面。错误可以重新输入(在原来界面)。
每天努力一点,加油!