Struts2简单开发

1.新建一个web项目,(导入相关包)目录结构如图:

Struts2简单开发

2.添加web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

id="WebApp_ID" version="3.0">

<display-name>struts</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

<display-name>Struts Blank</display-name>

<!-- 配置Struts核心过滤器 -->

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

 

</web-app>

3.创建一个Login类:

package com.dairui.Action;

 

public class LoginAction {

 

private String name;

private String password;

 

public String getName() {

return name;

}

 

public void setName(String name) {

this.name = name;

}

 

public String getPassword() {

return password;

}

 

public void setPassword(String password) {

this.password = password;

}

 

public String excute() {

if (name.equals("123") && password.equals("123")) {

 

return "success";

}

return "error";

}

 

}

4.在strut.xml文件中加入:(注意是在src目录下,且文件名一定要正确)

<?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="hello" namespace="/" extends="struts-default">

<action name="hello" class="com.dairui.Action.LoginAction"

method="excute">

<result name="success">/success.jsp</result>

<result name="error">/error.jsp</result>

</action>

 

</package>

 

</struts>

5.测试