日期:2014-05-16  浏览次数:20300 次

jsp+strut2.1使用

?

jsp+strut2.1使用

前几天写的都是c/c++类的给朋友们

现在给个b/s框架类的给朋友们分享一下,全是为了服务大家

?

源码+说明文件后面有下载......

?

我这里用的是myeclipse8.5

File->New->Web Project

?

1.

输入teststruct

这是一个基本的web项目,目前没有加入任何jar,如果你使用eclipse也是同样

建立一个空的web项目



?

?

2.

Myeclipse->Project Capabilities->Add Struts Capabilities

?






这样strut2所使用的jar,你就引入了

如果你用eclipse,引入你所需要的jar,把下面所有的jar,找到引入的到你的项目中

?

3.

修改struct.xml


<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

<constant name="struts.devMode" value="true" />

<constant name="struts.ui.theme" value="simple" />?

<!-- 解决表单换行 -->

<constant name="struts.action.extension" value="action,qvb" />?

<!-- ?使xxx.action 或是 xxxx.qvb 都是表单 -->

<package name="simple_qvb01" extends="struts-default">

<action name="process" class="com.qvb.first.processjava">

?<result>process_jsp.jsp</result>

</action>

</package>

</struts> ? ?

保存

我们这里表单的名加入了一个qvb自定义

主表单类,com.qvb.first.processjava我们一会再进行编写

结果映射的process_jsp.jsp我们一会再进行编写

这个struct.xml在生成后应该在classes中

也就是要和你生成的class在一个位置

4.

修改WebRoot中的index.jsp

如果你的标签库引入不正常,这里taglib就会有问题

?

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib uri="/struts-tags" prefix="s" %>

<html>

? <head>

? ? <title>Login</title>

? </head>

?

? <body>

? <s:form action="process.qvb">

? <table border='0' width='260' height='100' align='center'>?

? <tr>

? <td>

? ? <s:textfield name="name">用户名: </s:textfield>

? ?</td>

? </tr>

? <tr>

? <td> ?

? ? <s:password name="pwd">密&nbsp;&nbsp;&nbsp;&nbsp;码: </s:password>

? </td>

? </tr>

? <tr>

? <td>

? ? ? <s:submit value="登录" />

? </td>

? </tr> ?

? </table>

</s:form>

? </body>

</html>

保存

这里的name pwd 是表单元素的名称,在后面的com.qvb.first.processjava类中要一致

5.

 WEB-INF/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.StrutsPrepare