手把手教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配置文件



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

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

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
登录成功
body>
html>

error.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
登录失败
body>
html>

login.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
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


<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>struts2display-name>
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>
  <filter>
  <filter-name>strutsfilter-name>
  <filterclass>org.apache.struts2.dispatcher.FilterDispatcherfilter-class>
  filter>
  <filter-mapping>
  <filter-name>strutsfilter-name>
  <url-pattern>/*url-pattern>
  filter-mapping>
  <welcome-file-list>
  <welcome-file>index.jspwelcome-file>
  welcome-file-list>
web-app>

在web.xml中配置分发器,Struts1使用ActionServlet作为分发器,而Struts2使用Filter作为分发器,其中的是对应org.apache.struts2.dispatcher.FilterDispatcher类。
我文件部署的路径如图:
手把手教struts2入门案例(不用struts标签实现)_第1张图片

你可能感兴趣的:(struts2框架)