Eclipse + Struts

来源:互联网 发布:东南亚旅游 知乎 编辑:程序博客网 时间:2024/06/05 10:08

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

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

1. commons-logging-1.1.1.jar2. freemarker-2.3.163. ognl-3.0.1.jar4. struts2-core-2.2.3.1.jar5. 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发布,打开网页

原创粉丝点击