您的位置:首页 > 编程语言 > Java开发

struts2基础(1)

2015-06-04 15:05 369 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: