Java Struts2简单入门实例和登录实例

Java Struts2简单入门实例和登录实例

  1 jsp出发action
  2 struts2拦截请求,调用后台action
  3 action返回结果,由不同的jsp展现数据

项目结构:

src
      struts.xml
      
   com
       hellostruts2
           action
                  HelloStrutsAction.java
                  LoginAction.java
                  
           model
                   HelloMessage.java
                   
WebContent
       HelloStruts.jsp
       index.jsp
       
    login
           error.jsp
           login.jsp
           success.jsp
           
    WEB-INF
           web.xml
           
        classes
               logging.properties
               mess.properties
               mess_zh_CN.properties
               
        lib
                commons-fileupload-1.3.1.jar
                commons-io-2.2.jar
                commons-lang-2.4.jar
                commons-lang3-3.2.jar
                commons-logging-1.1.3.jar
                freemarker-2.3.22.jar
                javassist-3.11.0.GA.jar
                ognl-3.0.14.jar
                struts2-core-2.3.28.1.jar
                xwork-core-2.3.28.1.jar

 

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" 
   xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   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>Struts 2</display-name>
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
   <filter>
      <filter-name>struts2CleanupFilter</filter-name>
      <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
   </filter>
   <filter>
      <filter-name>struts2Filter</filter-name>
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
   </filter>

    <filter-mapping>
      <filter-name>struts2CleanupFilter</filter-name>
      <url-pattern>/*</url-pattern>
      <dispatcher>REQUEST</dispatcher>
      <dispatcher>FORWARD</dispatcher>
   </filter-mapping>
   <filter-mapping>
      <filter-name>struts2Filter</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>
View Code

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>
    <constant name="struts.devMode" value="true" />
    
    <constant name="struts.custom.i18n.resources" value="mess"/>
    <constant name="struts.i18n.encoding" value="utf-8"/>
    
    
    <package name="hellostruts" extends="struts-default">
        <action name="index">
            <result >/index.jsp</result>
        </action>
        <action name="hello" class="com.hellostruts2.action.HelloStrutsAction" method="execute">
            <result name="success">/HelloStruts.jsp</result>
        </action>
    </package>
    <package name="loginstruts" extends="struts-default">
        <action name="login" class="com.hellostruts2.action.LoginAction" method="execute">
            <result name="input">/login/login.jsp</result>
            <result name="success">/login/success.jsp</result>
            <result name="error">/login/error.jsp</result>
        </action>
    </package>
    
</struts>
<!-- 
<struts>
    <include file="my-struts1.xml"/>
    <include file="my-struts2.xml"/>
</struts>
-->
View Code

 

logging.properties

org.apache.catalina.core.ContainerBase.[Catalina].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].handlers = \java.util.logging.ConsoleHandler

mess.properties

loginPage=loginPage
errorPage=errorPage
successPage=succPage
errorTip=sorry\uFF0C login failed
successTip=welcome{0},login success
user=username
pass=password
login=login

mess_zh_CN.properties

loginPage=登陆界面
errorPage=失败界面
successPage=成功界面
errorTip=对不起,您不能登录!
successTip=欢迎,{0},您已经登录!
user=用户名
pass=密 码
login=登陆

 

入门实例:

HelloMessage.java

package com.hellostruts2.model;

public class HelloMessage {
    private String message;
    public HelloMessage(){
        setMessage("Hello struts2 model.");
    }
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
View Code

HelloStrutsAction.java

package com.hellostruts2.action;

import com.hellostruts2.model.HelloMessage;

public class HelloStrutsAction {

    private String name;
    private HelloMessage helloMessage;
    public String execute() throws Exception{
        helloMessage=new HelloMessage();
        helloMessage.setMessage("Hello struts model.");
        return "success";
    }
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name=name;
    }
    
    public HelloMessage getMessage(){
        return helloMessage;
    }
    public void setMessage(HelloMessage helloMessage){
        this.helloMessage=helloMessage;
    }
}
View Code

index.jsp

 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 2     pageEncoding="ISO-8859-1"%>
 3 <%@ taglib prefix="s" uri="/struts-tags"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 8 <title>Hello Struts2 Form</title>
 9 </head>
10 <body>
11     <h1>Hello Struts2 Form</h1>
12     <form action="hello">
13         <label for="name">Please enter your name</label><br/>
14         <input type="text" name="name"/>
15         <input type="submit" value="Say Hello"/>
16         <p><a href="<s:url action='hello'/>">Hello Struts Model</a></p>
17     </form>
18 </body>
19 </html>
View Code

HelloStruts.jsp

 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 2     pageEncoding="ISO-8859-1"%>
 3 <%@ taglib prefix="s" uri="/struts-tags" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 8 <title>Hello Struts2</title>
 9 </head>
10 <body>
11 <s:debug></s:debug>
12     Hello Struts2, <s:property value="name"/>
13     <br/>
14     Hello Model, <s:property value="message.message"></s:property>
15     ${message.message}
16 </body>
17 </html>
View Code

 

登录实例:

当login.jsp触发action时,就会向后抬发送login.action的请求,这个请求被后台拦截,交给struts.xml中配置的action处理。

LoginAction.java

package com.hellostruts2.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
    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;
    }
    public String execute() throws Exception{
        if(getUsername().equals("test") && getPassword().equals("test")){
            ActionContext.getContext().getSession().put("user", getUsername());
            return SUCCESS;
        }else{
            return ERROR;
        }
    }
    public void validate(){
        if(username ==null || username.trim().equals("")){
            addFieldError("username", "user name is required");
        }
        if(password.length()<3){
            addFieldError("password", "password must be more than 3");
        }
    }
}
View Code

login.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@taglib prefix="s" uri="/struts-tags" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title><s:text name="loginPage"/></title>
 9 </head>
10 <body>
11 <s:form action="login">
12     <s:textfield name="username" key="user"></s:textfield>
13     <s:textfield name="password" key="pass"></s:textfield>
14     <s:submit key="login"></s:submit>
15 </s:form>
16 </body>
17 </html>
View Code

success.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <%@taglib prefix="s" uri="/struts-tags" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 8 <title><s:text name="successPage"></s:text></title>
 9 </head>
10 <body>
11     <s:text name="successTip">
12         <s:param>${sessionScope.user}</s:param>
13     </s:text>
14 </body>
15 </html>
View Code

error.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <%@taglib prefix="s" uri="/struts-tags" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 8 <title><s:text name="errorPage"></s:text></title>
 9 </head>
10 <body>
11     <s:text name="errorTip"></s:text>
12 </body>
13 </html>

 

library下载:http://struts.apache.org/download.cgi

posted @ 2016-04-29 15:00  undefined?  阅读(2921)  评论(0编辑  收藏  举报