程序人生

Java EE框架struts2使用案例

时间:2017/4/21 9:47:05  作者:www.solgle.com  来源:solgle.com  查看:742  评论:0
内容摘要:这是一个简单struts的使用案例,仅用于参考,属于MVC层的C,即控制器,如果结合ajax,springMVC,这个框架就很少用了。
struts框架下载地址:http://struts.apache.org/ 及所需jar包如下
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.3.2.jar
commons-io-2.2.jar
commons-lang-2.4.jar
commons-lang3-3.2.jar
freemarker-2.3.22.jar
javassist-3.11.0.GA.jar
ognl-3.0.19.jar
struts2-core-2.3.32.jar
xwork-core-2.3.32.jar

根目录下web页面index.jsp简易代码
本文出自 http://www.solgle.com/news/357.html
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="ISO-8859-1"%>
<!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='p_action' method="post" >
   <input type="text" name="username"/><br/>  
   <input type="password" name="password"/><br/>  
   <input type="submit" value=“提交”/>  
</form>
</body>
</html>
 
WebContent\WEB-INF文件夹下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" version="3.0">
  <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>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
 
lib下面需要的struts2的jar包
 
源代码目录src下配置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>  
    <package name="struts2" namespace='/' extends="struts-default">  
        
        <action name="p_action" class="srvaction.SrvAction" >  
            <result name="sucess">index.jsp</result>  
            <result name="error">index.jsp</result> 
        </action>                   
    </package>   
</struts>     
 
相关java包和类的代码
package srvaction;
 
 
import hbnado.AdoStudent;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
 
 
 
public class SrvAction 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 {
try{
     System.out.println("enter username:"+this.getUsername());
 
    
        if (getUsername().equals("xingoo") && getPassword().equals("123")) {
            ActionContext.getContext().getSession().put("user", getUsername());                         
            return SUCCESS;
        }else{
            return ERROR;
        }
}catch(Exception e){
e.printStackTrace();
return ERROR;
}
    }
}
 
标签:java EE框架struts2使用案例 java框架 

solgle.com 版权所有,欢迎分享!!!

相关评论
 img1 img2 img3 img4 img5 img6 img7 img8 img9 img10
评论者:      验证码:  点击获取验证码
   Copyright © 2013-2028 solgle.com,All rights reserved.[solgle.com] 公安机关备案号:51010802000219
Email:solgle@solgle.com; weixin:cd1008610000 ICP:蜀ICP备14011070号-1