Struts2 无法访问直接通过URL访问jsp文件

来源:互联网 发布:虚拟漫游软件 编辑:程序博客网 时间:2024/05/16 17:09

Struts2 无法访问直接通过URL访问jsp文件

struts2配置文件如下:
<?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><package name="main" extends="struts-default"><action name="loginPerson"class="com.escape.action.LoginAction"><result name="success">/jsp/welcome.jsp</result><result name="login">/jsp/login.jsp</result></action></package></struts>    
action类如下:
package com.escape.action;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport {private String username;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;}private String password;//编写主方法public String execute(){if("tangbo".equals(username) && "123456".equals(password)){return SUCCESS;//这里直接当成string类型}return LOGIN; }}
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>Struts2MVCTest</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.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>*.action</url-pattern>  </filter-mapping></web-app>
login.jsp文件如下:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@ taglib uri="/struts-tags" prefix="s" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'login.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>        <s:form action="loginPerson">    <s:label value="login system"></s:label> <br/>    <s:textfield name="username" label="username"></s:textfield>    <s:textfield name="password" label="password"></s:textfield>    <s:submit value="login"></s:submit>    </s:form>  </body></html>
web project的文件目录如下:


浏览器访问jsp文件下的login.jsp发现错误如下:
只有通过http://localhost:8080/Struts2MVCTest/loginPerson.action才能正确访问。
查找原因:直接在jsp文件夹下新建一个jsp通过URL可以直接访问。网上搜索原因,部分人说是因为在jsp中加入了struts2标签库。查看了书的源例程之后看到struts配置如下:

要将login设置成全局result。发现还是不可以,最后再找原因原来是web.xml配置URLpattern时没有配置jsp文件夹,添加如下:
  <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>*.action</url-pattern>    <url-pattern>/jsp/*</url-pattern>  </filter-mapping>
然后可以直接通过URL:http://localhost:8080/Struts2MVCTest/jsp/login.jsp直接访问。
排除方法:1.排除web.xml配置问题 2.排除struts2配置问题




0 0