struts2基礎(1)


1、Struts2介紹
2、手動配置Struts2步驟
 <1>新建項目
 <2>導入所需jar包
 <3>配置web.xml
  <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 </filter>

 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

 <4>新建Struts2的配置文件:struts.xml
 <5>添加dtd
 <6>測試
3、 Struts1與Struts2的區別
 <1>配置不同:
  Struts1採用的是<servlet>標籤
  Struts2採用的是<filter>標籤
 <2>攔截不同
  Struts1攔截的是*.do
  struts2攔截的是*.action
 <3>
  struts1的action需要繼承action
  struts2的action不需要繼承任何類
 <4>
  struts1的action方法返回值是ActionForward
  struts2的action方法返回值是String
 <5>
  struts1的action中的方法有四個參數,與web是緊耦合
  Struts2的action中的方法沒有參數,與web是松耦合

示列代碼:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <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>

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>
 <package name="mypackage" extends="struts-default">
  <action name="studentsava" class="com.action.Savaaction" method="sava">
   <result name="success">/sava.jsp</result>
  </action>
 </package>
</struts>   
   

index.jsp

<body>
 <form action="studentsava.action" method="post">
  studentName:<input type="text" name="studentName"/><br/> 
  studentAge:<input type="text" name="studentAge"/><br/>
  studentSex:<input type="text" name="studentSex"/><br/>
  <input type="submit" value="保存"/>
 </form>
  </body>

SavaAction.java

package com.action;

public class Savaaction {
 private String studentName;
 private int studentAge;
 private String studentSex;
 
 public String getStudentName() {
  return studentName;
 }
 public void setStudentName(String studentName) {
  this.studentName = studentName;
 }
 public int getStudentAge() {
  return studentAge;
 }
 public void setStudentAge(int studentAge) {
  this.studentAge = studentAge;
 }
 public String getStudentSex() {
  return studentSex;
 }
 public void setStudentSex(String studentSex) {
  this.studentSex = studentSex;
 }
 public Savaaction(String studentName, int studentAge, String studentSex) {
  super();
  this.studentName = studentName;
  this.studentAge = studentAge;
  this.studentSex = studentSex;
 }
 public Savaaction() {
  super();
  // TODO Auto-generated constructor stub
 }
 
 public String sava(){
  System.out.println("姓名:"+studentName+" 年齡:"+studentAge+" 性別:"+studentSex);
  return "success";
 }

}

 

 

sava.jsp


  <body>
    <h1>學生信息</h1>
    studentName:${studentName}<br/>
    studentAge:${studentAge }<br/>
    studentSex:${studentSex }
  </body>

 


<body>
    <h1>學生信息</h1>
    studentName:${studentName}<br/>
    studentAge:${studentAge }<br/>
    studentSex:${studentSex }
  </body>

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章