Eclipse + Struts

1. 新建 Dynamic Web Project[Sample],并配置Tomcat服务器

2. 复制JAR包至“WEB-INF/lib”目录下,如

1. commons-logging-1.1.1.jar
2. freemarker-2.3.16
3. ognl-3.0.1.jar
4. struts2-core-2.2.3.1.jar
5. xwork-core-2.2.3.1.jar

3. 修改“web.xml”,主要添加“filter”

    <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>Sample.jsp</welcome-file>
    </welcome-file-list>

4. 建包“com.pocky.struts”,并生成action类“Sample”

package com.pocky.struts;

public class Sample {
	private String username;
	private String password;

	public String execute() {

		if (this.username.equals("sample") && this.password.equals("sample")) {
			return "success";
		} else {
			return "error";
		}
	}

	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;
	}
}

5. 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>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />

    <package name="default" extends="struts-default" namespace="/">
        <action name="sample" method="execute"
            class="com.pocky.struts.Sample">
            <result name="success">Welcome.jsp</result>
            <result name="error">Sample.jsp</result>
        </action>
    </package>

    <!-- Add packages here -->

</struts>

6. 新建“Sample.jsp”,“Welcome.jsp”

<!-- Sample.jsp -->
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts 2 - Login Application | Sample</title>
</head>

<body>
	<h2>Struts 2 - Login Application</h2>
	<s:actionerror />
	<s:form action="sample" method="post">
		<s:textfield name="username" label="User ID:" size="20" />
		<s:password name="password" lable="Password" size="20" />
		<s:submit method="execute" label="Submit to Login" align="center" />
	</s:form>
</body>
</html>

<!-- Welcome.jsp -->
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Welcome</title>
</head>

<body>
	<h2>
		Welcome,
		<s:property value="username" />
		...!
	</h2>
</body>
</html>

7. Tomcat发布,打开网页

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值