struts2的配置步骤

1.现在struts2的压缩包,并将其解压;

2.将struts2解压包下的apps目录里面struts2-blank.war文件放到 tomcat安装目录下的webapp下面;然后启动tomcat,将struts2-blank.war解压开;

3.在myeclipse里 新建一个web项目;

4.将struts2-blank.war解压目录下lib文件下的 jar文件拷贝到myeclipse项目中;

5.配置web.xml文件

      

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>(配置的过滤器类是org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 和原来不一样 原来是 org.apache.struts2.dispatcher.FileDispatcher 这是 因为从struts-2.1.3以后  org.apache.struts2.dispatcher.FileDispatcher 被注释为过时;虽然起作用,但不建议使用 )
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

6.编写LoginAction

package com.gsww.kingreturns.struts2.excise;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {//该类继承了ActionSupport类。这样就可以直接使用SUCCESS, LOGIN等变量和重写execute等方法

 private static final long serialVersionUID = 1L;
 private String username;
 private String password;
 
 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;
 }

 @Override
 public String execute() throws Exception {
  if("haha".equals(username) && "hehe".equals(password))//如果登录的用户名=haha并且密码=hehe,就返回SUCCESS;否则,返回LOGIN
   return SUCCESS;
  return LOGIN;
 }
}

7.配置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="default" namespace="/" extends="struts-default">
<action name="login" class="com.cn.test.LoginAction" method="execute">
<result name="success">/welcome.jsp</result>
<result name="login">/login.jsp</result>
</action>
</package>
</struts>

8.几个Jsp文件

Login.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Login</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="/login" method="post">
<s:label value="系统登陆"></s:label>
<s:textfield name="username" label="账号" />
<s:password name="password" label="密码" />
<s:submit value="登录" />
</s:form>

</body>
</html>

welcome.jsp

省略。。。。。

  

原文地址:https://www.cnblogs.com/GodFather001/p/2272353.html