欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts2学习笔记一(第1讲.Struts2入门与配置)

程序员文章站 2022-05-28 15:57:47
...
[b][size=xx-large]struts2学习笔记一(第1讲.Struts2入门与配置)[/size][/b]
声明:此struts2学习资源来源于浪曦网视频,做一系列笔记的目的是强化自己的记忆,也能方便自己以后的查阅,如有得罪,请多包涵!!!
一、开发环境
IDE:MyEclipse6.0版本
Servlet Container:Tomcat6.0
二、创建web project工程
名称:struts2
1、功能:完成一个用户简单的登录
创建一个登录页面login.jsp

<body>
<form action="login.action" method="post">
username:<input type="text" name="username"> <br>
password:<input type="password" name="password"><br>
<input type="submit" value="sumbit">
</form>
</body>

2、配置C:\Tomcat 6.0\conf\server.xml

<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">

<Context path="/struts2" docBase="F:\workspace\struts2\WebRoot"

reloadable="true"/>
</Host>


配置好jdk和tomcat。
3、配置struts2:
首次要添加的5个jar包名:[color=red]
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.14.jar
xwork-2.0.7.jar[/color]
4、配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<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><!--表示客服端发送过来

的所有请求都必须由FilterDispatcher过滤器来过滤-->
</filter-mapping>
</web-app>

5、(1)在src根目录下创建struts.xml(MyEclipse会自动的保存到

F:\workspace\struts2\WebRoot\WEB-INF\classes目录下),在下载的struts2

的文件中struts-2.0.14\apps中的任意的war打开,找到对应的struts.xml文

件拷贝struts的版本号:

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

(2)在src下创建com.test.action包:创建LoginAction类:

package com.test.action;

public class LoginAction
{
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
{
return "success";
}}


(3)创建result.jsp页面:

<body>

username:${requestScope.username }<br>
password:${requestScope.password }

</body>
</html>

(4)配置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" extends="struts-default">

<action name="login" class="com.test.action.LoginAction">
<result name="success">/result.jsp</result>
</action>

</package>

</struts>

效果:

[img]http://dl.iteye.com/upload/attachment/382182/56ffbfd9-3db7-3463-8a67-f498cdeeac20.jpg[/img]

[img]http://dl.iteye.com/upload/attachment/382184/e063b9a0-18c6-3694-93e3-16ab619d8ea9.jpg[/img]

[img]http://dl.iteye.com/upload/attachment/382186/8a25bf85-6f59-3983-9b7e-b6996bd9fdf8.jpg[/img]