简体   繁体   中英

SpringMVC Controller Error

I've been tinkering on this error ever since Friday and until now I still haven't fixed it. I've based this SpringMVC project on an assignment that I've worked on last year that works. And added a few new things. So I have this login page, I didn't add yet the security. I just wanted to see if my FrontEnd and BakcEnd will meet if I do transaction such as checking the user if it exists on the Database and forward it to the mainpage if it does. Here are some codes snippets:

For the TestController.java

@Controller
public class TestController {

@Autowired
private UserService userService;

@RequestMapping(value = "index")
public ModelAndView index(HttpServletRequest request, ModelMap model) {
    System.out.println("Index !");
    return new ModelAndView("index", "user", new User());
}

@RequestMapping(value = "login", method = RequestMethod.POST)
public ModelAndView login(HttpServletRequest request,
        HttpServletResponse res) {
    System.out.println("inside login");
    String userName = request.getParameter("username");
    String password = request.getParameter("password");
    System.out.println(userName);
    System.out.println(password);

    User user = new User();
    user.setUsername(userName);
    user.setPassword(password);

    if (userService.userLogin(user)) {
        return new ModelAndView("mainpage");
    } else {
        return new ModelAndView("index");
    }
}

The index.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page import="javax.servlet.http.HttpSession" %>
<html>
<head>
<title>KSPEAK CMS V1.0</title>
<!-- Bootstrap core CSS -->
<link href="<c:url value='/resources/bootstrap/css/bootstrap.min.css'/>"
rel="stylesheet">
<!-- Custom styles for this template -->
<link href="<c:url value='/resources/bootstrap/css/signin.css'/>"
rel="stylesheet">
<body>

<div class="container">

    <form:form class="form-signin" method="POST" action="login" commandName="user"  >
        <h2 class="form-signin-heading">Please sign in</h2>
        <label for="inputUserName" class="sr-only">UserName</label> <form:input
            type="text" name="username" class="form-control" path="username"
            placeholder="UserName" required="required" autofocus="autofocus"/> <label
            for="inputPassword" class="sr-only">Password</label> <form:input
            type="password" name="password" class="form-control" path="password"
            placeholder="Password" required="required"/>
        <input class="btn btn-lg btn-primary btn-block" type="submit" value="login">
    </form:form>
</div>
</body>
</head>
</html>

My 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-a      pp_2_5.xsd">

<servlet>
    <servlet-name>myBatisServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/springConfig.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>myBatisServlet</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>

My SpringConfig.xml (I've redacted some things on this part because it messes with the layout of the post it's just the xmlns and schema locations)

<mvc:annotation-driven />

<context:component-scan base-package="com.cms.*" />

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc:hsqldb:hsql://localhost:9001/" />
    <property name="username" value="SA" />
    <property name="password" value="" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="typeAliasesPackage" value="com.cms.model" />
    <property name="mapperLocations" value="classpath*:com/cms/mappers/*.xml" />
</bean>

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory" />
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.cms.mappers" />
</bean>
</beans>

When running the app. It successfully loads the index.jsp and pass through my first sysout on the controller. As shown below:

在此处输入图片说明

Let's say I've entered an existing user it should redirect to the mainpage. But here's a picture of what it does 在此处输入图片说明

It doesn't go the login method I've placed on the controller.

Somethings that I had done so far is adding / to the value of the @RequestMapping for login but still same error. Tried manually typing the login to the url bar same error, tried to create another method in the controller same error. As far as what can I see it only recognizes the index. So yeah, I think I need some help. As per usual I can upload any files from my project that you need to see. Thanks.

<servlet-mapping> <servlet-name>myBatisServlet</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping>

This means spring DispatcherServlet will be called for the requests matching the patter *.html, But you are sending request to the application as login, which will not be handled by DispatcherServlet.

There are two possible solutions to your problem

1. Replace *.html with / in <url-pattern> 2. Change you request to match the pattern *.html

Just an idea:

Remove the .html from this

<servlet-mapping> <servlet-name>myBatisServlet</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping>

the reason the index worked is that you have in the welcome file list index.html and when you access the application with the base url ( yourwebpage/ ) it looked at the welcome file list, and tried to get yourwebpage/index.html , and you have a requestmapping for "index" and url pattern *.html, and it gives index.html, and the login requestmapping will become login.html

So if you leave the *.html url pattern, your urls will be the value attribute of the @RequestMapping + .html

if you delete the .html they will be just the value attribute of the @RequestMapping

I would suggest to try below.

You are using commandName="user" in your index.jsp ,which has two attribute's username and password .

As per your JSP,Make below changes in your server side.

1 You should have a java class User.java having same attribute as path in your index.jsp

class User{

     private username; // name must match path in your form inside index.jsp
     private password;
     //getter && setter

   }

2 Then pass this object as parameter inside your login method,

 @RequestMapping(value = "login", method = RequestMethod.POST)
public ModelAndView login( @ModelAttribute User user) {
   // do your stuff
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM