Struts2简单配置

Posted 夕冰

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2简单配置相关的知识,希望对你有一定的参考价值。

Struts2简单配置

Struts2简单配置,实现用户登录

各级文件位置

                             




所需架包:




1.1  web.xml文件

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

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"

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

    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee

    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

  <!-- 与项目名称一致 -->

  <display-name>jsp</display-name>

  <!-- 设置url不写文件名时,默认访问的页面 -->

  <welcome-file-list>

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

  </welcome-file-list>

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




1.2  struts.xml文件

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

    <!-- 指定编码 -->

    <constant name="struts.i18n.encoding"  value="UTF-8"></constant>

    <!-- 指定全局国际化资源文件 -->

    <constant name="struts.custom.i18n.resources"  value="mess"></constant>

    <constant name="struts.devMode"  value="true"></constant>

    <constant name="struts.enable.DynamicMethodInvocation"  value="false"></constant>

    <package name="test"  extends="struts-default">

        <action name="login" class="org.cgx.vo.LoginAction">

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

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

        </action>

    </package>

</struts>





1.3  常量配置文件mess.properties

failTip=sorry,loginfailed

succTip=welcome{0},loginsuccess

user=username

pass=password

login=login

中文常量配置文件mess_zh_CN.properties

failTip=\u5BF9\u4E0D\u8D77\uFF0C\u60A8\u767B\u5F55\u5931\u8D25\uFF01

succTip=\u6B22\u8FCE\uFF0C{0}\uFF0C\u767B\u5F55\u6210\u529F\uFF01

user=\u7528\u6237\u540D

pass=\u5BC6\u7801

login=\u767B\u5F55

 




1.4  Action登录处理LoginAction.java

package org.cgx.vo;

 

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

 

publicclass LoginAction extends ActionSupport{

    private String username;

    private String password;

       

    public String execute(){

       System.out.println("11111111111111");

       if(username.equals("admin") && password.equals("123")){

           ActionContext.getContext().getSession().put("user", getUsername());

           return"success";

       }else{

           return"fail";

       }

    }

       

    public String getUsername() {

    returnusername;

    }

    publicvoid setUsername(String username)  {

    this.username = username;

    }

    public String getPassword() {

    returnpassword;

    }

    publicvoid setPassword(String password)  {

    this.password = password;

    }

 

}

 




1.5  登录表单index.jsp

<%@ page language="java"  import="java.util.*" pageEncoding="UTF-8"%>

<%@taglib prefix="s" uri="/struts-tags"  %>

<!DOCTYPE html  PUBLIC "-//W3C//DTD  HTML 4.01 Transitional//EN">

<html>

    <head>

    <title>Insert title here</title>

    </head>

    <body>

       <s:form action="/login">

            <s:textfield name="username" key="user"/>

            <s:textfield name="password" key="pass"/>

            <s:submit key="login"/>

       </s:form>

    </body>

</html>

 




1.6  登录成功页面welcome.jsp

<%@ page language="java"  import="java.util.*" pageEncoding="utf-8"%>

<%@taglib prefix="s" uri="/struts-tags"  %>

<!DOCTYPE HTML  PUBLIC "-//W3C//DTD  HTML 4.01 Transitional//EN">

<html>

  <head> 

    <title>My JSP 'welcome.jsp' starting page</title>

  </head>

 

  <body>

     <s:text name="succTip">

        <s:param>${sessionScope.user}</s:param>

     </s:text>

  </body>

</html>

 




1.7  登录失败页面error.jsp

<%@ page language="java"  import="java.util.*" pageEncoding="utf-8"%>

<%@taglib prefix="s" uri="/struts-tags"  %>

<!DOCTYPE HTML  PUBLIC "-//W3C//DTD  HTML 4.01 Transitional//EN">

<html>

  <head>

    <title>My JSP 'error.jsp' starting page</title>

  </head>

  <body>

        <s:text name="failTip"></s:text>

  </body>

</html>



以上是关于Struts2简单配置的主要内容,如果未能解决你的问题,请参考以下文章

Struts2的配置和一个简单的例子

struts2简单入门-配置文件说明

Struts2和SpringMVC简单配置以及区别总结

struts2相关简单介绍

struts2简单入门-Action的三种配置方式

搭建一个简单的Struts2应用