struts2的详细配置

1.新建web工程
2.导入jar包struts2的详细配置_第1张图片
项目结构图:struts2的详细配置_第2张图片

直接来源码吧

package zh.struts.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;

@Namespace("/user")
@Results({ @Result(name = "success", location = "/welcome.jsp"), @Result(name = "login", location = "/login.jsp") })
public class LoginAction extends ActionSupport {

    /**
     * 
     */
    private static final long serialVersionUID = -8275633865217677525L;
    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;
    }

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

}

struts.xml



<struts>
    
    <constant name="struts.action.extension" value="do" />

    
    
    
    
    
    
    
    
struts>

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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>sshDemodisplay-name>
    <welcome-file-list>
        <welcome-file>login.jspwelcome-file>
    welcome-file-list>
     <filter>
        <filter-name>action2filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilterfilter-class>
    filter>

    <filter-mapping>
        <filter-name>action2filter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
web-app>

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
   <s:form action="/user/login.do" 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

<%@ 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>
欢迎${username}
body>
html>

你可能感兴趣的:(struts2)