手把手教struts2入门案例(不用struts标签实现)

①先将struts2需要的jar包放到/WEB-INF/lib下面(jar包下载地址这里写链接内容)
注意:如果项目没有用到Plaxus、Sitegraph、Spring,添加时需要将struts2-plexus-plugin-2.0.11.jar、struts-sitegraph-plugin-2.0.11.jar、struts2-jsf-plugin-2.0.11.1.jar和struts-spring-plugin.2.0.11.jar删掉。否则启动服务器时会抛出异常。
②实现登录功能的Action

package com.cph.actions;

import com.opensymphony.xwork2.ActionSupport;
//struts2的Action继承com.opensymphony.xwork2.ActionSupport
public class LoginAction extends ActionSupport {
//与login.jsp中的属性名为username对应
    private String username;  
    private String pwd;
    @Override
    public String execute() throws Exception {
    //主方法
        // TODO Auto-generated method stub
        if("cph".equals(username)&&"123".equals(pwd)){
        //在struts.xml中配置的name="success"中的result标签
            return SUCCESS;
        }else {
        //在struts.xml中配置的name="error"中的result标签
            return ERROR;
        }
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}

③Struts2配置文件

<?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="main" extends="struts-default">
<!--
所有的result、action等必须要配置在package中,package具有继承性,子package继承父package。
-->
<global-results>
<result name="error">/WEB-INF/error.jsp</result>
</global-results>
<action name="personLogin" class="com.cph.actions.LoginAction">
<result name="success">/WEB-INF/success.jsp</result>
</action>
</package>
</struts>

④jsp的登录页面(不用struts标签实现)
success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登录成功
</body>
</html>

error.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登录失败
</body>
</html>

login.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body> 
<form action="personLogin.action" method="post">
<input type="text" name="username" />
<input type="password" name="pwd"/>
<input type="submit" name="submit" value="提交"/>
</form>
</body>
</html>

注意login.jsp中的form中的action中的名字对应struts.xml中action的名字,同时要在后面加上.action,否则会出现路径不存在的问题。
⑤配置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>struts2</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>
  <filter>
  <filter-name>struts</filter-name>
  <filterclass>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>struts</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

在web.xml中配置分发器,Struts1使用ActionServlet作为分发器,而Struts2使用Filter作为分发器,其中的是对应org.apache.struts2.dispatcher.FilterDispatcher类。
我文件部署的路径如图:
这里写图片描述

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页