Spring URI - 控制器映射

时间:2015-06-15 18:26:27

标签: spring google-app-engine spring-mvc

我在安装Spring Security之前有一个控制器映射。我正在学习MVC和Spring。

现在我收到一个PageNotFoundException,说无法找到路径。如果你看到我的控制器它映射到/ user和方法来获取/ list中的数据。

我不确定问题是什么。任何帮助都会有用。

以下是信息:

MVC-调度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

<!-- Bean to show you Di in GAE, via Spring, also init the UserController -->
<!--  <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
 -->
 <context:component-scan base-package="com.principalmvl.lojackmykids">
 <context:include-filter type="regex" expression="(service|constroller)\..*"/>
 </context:component-scan>
    <!-- Enables JSR-303 -->
<mvc:annotation-driven/>
<context:annotation-config />


<bean class="com.google.appengine.api.users.UserServiceFactory"
    factory-method="getUserService" />
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

的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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml /WEB-INF/applicationContext-security.xml</param-value>
</context-param>
<sessions-enabled>true</sessions-enabled>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
    <filter-name>datastoreFilter</filter-name>
    <filter-class>org.slim3.datastore.DatastoreFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>datastoreFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>

<security-constraint>
    <web-resource-collection>
        <web-resource-name>profile</web-resource-name>
        <url-pattern>/profile/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>*</role-name>
    </auth-constraint>
</security-constraint>

<security-constraint>
    <web-resource-collection>
        <web-resource-name>admin</web-resource-name>
        <url-pattern>/admin/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>admin</role-name>
    </auth-constraint>
</security-constraint>

UserController.java

 @Controller
 @RequestMapping("/user")
 public class UserController {

@RequestMapping(value = "/addUser", method = RequestMethod.GET)
public String getAddUserPage(ModelMap model) {

    return "add";

}


// get all users

@RequestMapping(value = "/list", method = RequestMethod.GET)
@ResponseBody
public  List<Contact> listCustomer( ModelMap model) {

    List<Contact> users = Datastore.query(Contact.class).asList();

    model.addAttribute("userList", users);

    return users;

 }
}

LoginController.Java [这是第一个被调用的控制器]

@Controller
public class LoginController {

private static final Logger log = Logger.getLogger(LookupUserServlet.class
    .getName());

@RequestMapping(value = "/", method = RequestMethod.GET)
public String landing() {
    return "landing";
}


@RequestMapping(value = "/home.jsp", method = RequestMethod.GET)
public String home() {

    return "home";
}

@RequestMapping(value = "/disabled.jsp", method = RequestMethod.GET)
public String disabled() {
    return "disabled";
}

@RequestMapping(value = "/logout.jsp", method = RequestMethod.GET)
public void logout(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    request.getSession().invalidate();

    String logoutUrl = UserServiceFactory.getUserService().createLogoutURL(
            "/loggedout.jsp");

    response.sendRedirect(logoutUrl);
}

@RequestMapping(value = "/loggedout.jsp", method = RequestMethod.GET)
public String loggedOut() {
    return "loggedout";
}


@RequestMapping("/ListUsers")
public String ListUser() {

    return "users/ListUsers";

}

@RequestMapping("/SendAll")
public String SendAll() {

    return "sendmessage/SendAll";

}

在我的ListUsers方法中,我应该重定向到名为UserController的控制器吗?

3 个答案:

答案 0 :(得分:0)

您的清单有额外/

@RequestMapping(value = "/list/", method = RequestMethod.POST)

将其更改为

@RequestMapping(value = "/list", method = RequestMethod.POST)

假设您提交的表单指向“/ list”

答案 1 :(得分:0)

所以我将URI映射移动到名为LoginController的控制器,并且请求有效。我现在的问题是,为什么我的UserController没有被拿起来。我在所有基础软件包和我的控制器中都进行了上下文扫描扫描。

有没有人有想法?

答案 2 :(得分:0)

经过更多调查。我相信Eclipse对Java类做了些什么。当我查看Java类以及Eclipse如何对其进行注释时,它没有相应的&#34;曲折的&#34;这表明了班上的承包商。

我创建了另一个Java类并添加了代码并且它运行正常。